这是我的XML:
<LIGHT_INFORMATION_LIST>
<LIGHT_INFORMATION>
<LIGHT_CHARACTERISTICS>Al</LIGHT_CHARACTERISTICS>
<LIGHT_COLOUR>W-G</LIGHT_COLOUR>
</LIGHT_INFORMATION>
<LIGHT_INFORMATION>
<LIGHT_CHARACTERISTICS>Al</LIGHT_CHARACTERISTICS>
<LIGHT_COLOUR>W-R</LIGHT_COLOUR>
</LIGHT_INFORMATION>
<LIGHT_INFORMATION>
<LIGHT_CHARACTERISTICS>F</LIGHT_CHARACTERISTICS>
<LIGHT_COLOUR>R</LIGHT_COLOUR>
</LIGHT_INFORMATION>
<LIGHT_INFORMATION>
<LIGHT_CHARACTERISTICS>F</LIGHT_CHARACTERISTICS>
<LIGHT_COLOUR>G</LIGHT_COLOUR>
</LIGHT_INFORMATION>
<LIGHT_INFORMATION>
<LIGHT_CHARACTERISTICS>F</LIGHT_CHARACTERISTICS>
<LIGHT_COLOUR>W</LIGHT_COLOUR>
</LIGHT_INFORMATION>
</LIGHT_INFORMATION_LIST>
我会插入一个控件:
如果该LIGHT_INFORMATION_LIST中的LIGHT_COLOR的至少一个元素包含"-"
我试过了,但完全错了:
<xsl:choose>
<xsl:when test="not(contains(.,'-'))">
<!-- do A -->
</xsl:when>
<xsl:otherwise>
<!-- do B -->
</xsl:otherwise>
</xsl:choose>
这实际上是我的XSLT代码:
<xsl:for-each select="LIGHT_INFORMATION">
<xsl:for-each select="LIGHT_COLOUR">
<xsl:choose>
<xsl:when test="not(contains(.,'-'))"> <!-- This is the test Required -->
<xsl:choose>
<xsl:when test="following-sibling::LIGHT_RANGE >= 15">
<span style="font-family:Univers Condensed; font-weight:bold;">
<xsl:if test="not(preceding::LIGHT_COLOUR[1][preceding::IMMUTABLE_ID = $EF])">
<xsl:value-of select="."/>
</xsl:if>
<xsl:if test=". != preceding::LIGHT_COLOUR[1][preceding::IMMUTABLE_ID = $EF]">
<xsl:value-of select="."/>
</xsl:if>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:if test="not(preceding::LIGHT_COLOUR[1][preceding::IMMUTABLE_ID = $EF])">
<xsl:value-of select="."/>
</xsl:if>
<xsl:if test=". != preceding::LIGHT_COLOUR[1][preceding::IMMUTABLE_ID = $EF]">
<xsl:value-of select="."/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
ciao <!-- I would insert this control here if LIGHT_COLOUR contain '-' -->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
现在的输出是:
ciao ciao R G W
如果LIGHT_INFORMATION_LIST的这个节点中只有一个元素包含在LIGHT_COLOUR'-"write"Ciao"中,我会这样做
Thi是解决方案:
<xsl:for-each select="LIGHT_INFORMATION">
<xsl:choose>
<xsl:when test="not(contains(parent::LIGHT_INFORMATION_LIST//LIGHT_COLOUR, '-'))"> <!-- This is the solution -->
<xsl:for-each select="LIGHT_COLOUR">
<xsl:choose>
<xsl:when test="following-sibling::LIGHT_RANGE >= 15">
<span style="font-family:Univers Condensed; font-weight:bold;">
<xsl:if test="not(preceding::LIGHT_COLOUR[1][preceding::IMMUTABLE_ID = $EF])">
<xsl:value-of select="."/>
</xsl:if>
<xsl:if test=". != preceding::LIGHT_COLOUR[1][preceding::IMMUTABLE_ID = $EF]">
<xsl:value-of select="."/>
</xsl:if>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:if test="not(preceding::LIGHT_COLOUR[1][preceding::IMMUTABLE_ID = $EF])">
<xsl:value-of select="."/>
</xsl:if>
<xsl:if test=". != preceding::LIGHT_COLOUR[1][preceding::IMMUTABLE_ID = $EF]">
<xsl:value-of select="."/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
Ciao
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
将控件插入头部并使用PARENT。
以下同级节点只有在同一级别上才能找到LIGHT_COLOR(即当前节点是LIGHT_HARACTERISTIC节点,即使在那时它也只能找到一个。
要查看所有这些节点,需要指定一个包含所有这些节点的路径,无论是全局路径还是相对于当前节点的路径。在这种情况下:
/LIGHT_INFORMATION_LIST/LIGHT_INFORMATION/LIGHT_COLOUR
此路径将选择所有LIGHT_COLOR节点。现在,如果每个节点不包含"-":,我们想过滤掉它
/LIGHT_INFORMATION_LIST/LIGHT_INFORMATION/LIGHT_COLOUR[contains(., "-")]
我们实际上对任何一个单独的节点都不感兴趣,我们只想知道有多少,所以:
count (/LIGHT_INFORMATION_LIST/LIGHT_INFORMATION/LIGHT_COLOUR[contains(., "-")])
因此,您的支票将是:
<xsl:when test='count (/LIGHT_INFORMATION_LIST/LIGHT_INFORMATION/LIGHT_COLOUR[contains(., "-")])'>
*编辑:修订问题的修订答案*如果你想要一个控件,那么你只想做一次,所以把它移出循环:
<!-- Do this where we can see all of the LIGHT_COLOR tags -->
<xsl:if test="contains(LIGHT_INFORMATION/LIGHT_COLOR, '-')">
Ciao
</xsl:if>
<xsl:for-each select="LIGHT_INFORMATION">
<!-- as per your existing code. Remove the "otherwise" clause that is creating multiple controls. -->