嗨,我想得到的文件,其中有类型的html。我很好。但是我的问题是,当没有html文件时,它显示为空。但是我想显示一些信息,比如“没有提交htmls”。我会在普通编程语言中使用计数器变量。在这里我做不到,请帮我解决这个问题。谢谢
注意:需要XSLT1.0。
xml:
<?xml version="1.0" encoding="UTF-8"?>
<Files>
<Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="MS/homedemo.html" type="html" />
<Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="MS/test125.html" type="html" />
<Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="iwov-resources/css/akz.css" type="css" />
<Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="iwov-resources/css/animate.css" type="css" />
<Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="iwov-resources/css/base.css" type="css" />
<Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="iwov-resources/css/font-awesome-ie7.css" type="css" />
</Files>xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<p>
<xsl:apply-templates select="//Files" />
</p>
</html>
</xsl:template>
<xsl:template match="//Files">
<h1 style="background: #979797;border-radius: 5px;color: #FFF;text-shadow: 1px 1px 1px #000;padding: 5px;font-size:16px;">US files are now live and you can access them using the following URLs:</h1>
<ol>
<xsl:for-each select="//Files/Path">
<xsl:variable name="type">
<xsl:choose>
<xsl:when test="@type">
<xsl:value-of select="@type" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'html'" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="path" select="@path" />
<xsl:choose>
<xsl:when test="$type='html'">
<li>
<a class="iw-base-link">
<xsl:attribute name="href">
<xsl:value-of select="$path"/>
</xsl:attribute>
<xsl:value-of select="$path" />
</a>
</li>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</ol>
</xsl:template>
</xsl:stylesheet>当我们有html文件时,然后显示如下内容:
US files are now live and you can access them using the following URLs:
1.MS/homedemo.html
2.MS/test125.html当我们没有html文件类型时,然后显示如下内容:
US files are now live and you can access them using the following URLs:
NO html files were submitted.发布于 2014-02-14 13:57:54
XSLT比需要的要复杂一些。而不是在每个路径上执行一个xsl: for -,然后在循环中为该类型添加一个测试,而是将逻辑添加到xsl:for-本身的“选择”中。
<xsl:for-each select="Path[@type='html' or not(@type)]">注意,这是一个“相对”表达式,相对于您当前所处的Files元素。
这意味着您不需要循环中类型的变量声明,也不需要xsl:。不过,更重要的是,它确实意味着您可以实际使用一个变量来保存要迭代的节点。
<xsl:variable name="files" select="Path[@type='html' or not(@type)]" /> 然后,可以使用xsl:选择来检查其中是否有任何节点。
<xsl:choose>
<xsl:when test="count($files) = 0">
No files
</xsl:when>
<xsl:otherwise>
<!-- You existing loop here -->
</xsl:otherwise>
<xsl:choose>试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<p>
<xsl:apply-templates select="//Files" />
</p>
</html>
</xsl:template>
<xsl:template match="//Files">
<xsl:variable name="files" select="Path[@type='html' or not(@type)]" />
<xsl:choose>
<xsl:when test="count($files) = 0">No files</xsl:when>
<xsl:otherwise>
<h1 style="background: #979797;border-radius: 5px;color: #FFF;text-shadow: 1px 1px 1px #000;padding: 5px;font-size:16px;">US files are now live and you can access them using the following URLs:</h1>
<ol>
<xsl:for-each select="$files">
<xsl:variable name="path" select="@path" />
<li>
<a class="iw-base-link" name="{$path}">
<xsl:value-of select="$path" />
</a>
</li>
</xsl:for-each>
</ol>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>还请注意在创建超链接时使用了“属性值模板”,从而使XSLT更加简单。大括号表示要计算的表达式,而不是字面意义上的输出。
发布于 2014-02-14 13:57:39
更简单些的怎么样?
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="HTMLfiles" select="/Files/Path[@type='html']" />
<xsl:template match="/">
<html>
<xsl:choose>
<xsl:when test="$HTMLfiles">
<h1 style="background: #979797;border-radius: 5px;color: #FFF;text-shadow: 1px 1px 1px #000;padding: 5px;font-size:16px;">US files are now live and you can access them using the following URLs:</h1>
<ol>
<xsl:apply-templates select="$HTMLfiles" />
</ol>
</xsl:when>
<xsl:otherwise>
<h1 style="background: #979797;border-radius: 5px;color: #FFF;text-shadow: 1px 1px 1px #000;padding: 5px;font-size:16px;">NO html files were submitted.</h1>
</xsl:otherwise>
</xsl:choose>
</html>
</xsl:template>
<xsl:template match="Path">
<li>
<a class="iw-base-link">
<xsl:attribute name="href">
<xsl:value-of select="@path"/>
</xsl:attribute>
<xsl:value-of select="@path" />
</a>
</li>
</xsl:template>
</xsl:stylesheet>请注意,XSLT在<p>元素中放置了一个<p>;我不确定这是件好事。
发布于 2014-02-14 13:49:10
xpath中实际上有一个count()函数:
<ol>
<xsl:choose>
<xsl:when test="count(//Files/Path) > 0">
<xsl:for-each select="//Files/Path">
..........
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
NO html files were submitted.
</xsl:otherwise>
</xsl:choose>
</ol>所以您只需编写一个when,otherwise大小写:)
https://stackoverflow.com/questions/21780854
复制相似问题