检查至少有一个孩子是否具有xslt 1.0的特定值

时间:2014-10-08 09:26:13

标签: xslt xslt-1.0

这是我的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_COLOUR元素包含' - '

我试过这个,但完全错了:

<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中,我会这样做 - ''写'Ciao'

2 个答案:

答案 0 :(得分:0)

这是解决方案:

<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。

答案 1 :(得分:-1)

follow-sibling只会找到LIGHT_COLOR,如果它在同一级别上(即当前节点是LIGHT_CHARACTERISTIC节点,即使那样它也只能找到一个。

要查看所有这些内容,您需要指定一个包含所有这些路径的路径,全局或相对于当前节点。在这种情况下:

/LIGHT_INFORMATION_LIST/LIGHT_INFORMATION/LIGHT_COLOUR

此路径将选择所有LIGHT_COLOR节点。现在我们想要过滤掉每个节点,如果它不包含&#34; - &#34;:

/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. -->