检查至少一个元素是否不同

时间:2014-10-29 14:40:08

标签: xslt xslt-1.0

这是我的XML:

<SECTION_CONTENT_LIST_ITEM>  <------- First Group
    <NTC_LIGHTLISTPRODUCT>
        <IMMUTABLE_ID>5585</IMMUTABLE_ID>
        <INTERNATIONAL_LIGHT_NUMBER>E1283</INTERNATIONAL_LIGHT_NUMBER>
        <VICINITY>LA SPEZIA</VICINITY>
        <LIGHT_INFORMATION_LIST>
            <LIGHT_INFORMATION>
                <LIGHT_COLOUR>G</LIGHT_COLOUR>  <-- LIGHT_COLOUR G
                <LIGHT_HEIGHT>8</LIGHT_HEIGHT>
                <LIGHT_RANGE>3</LIGHT_RANGE>    <-- LIGHT_RANGE 3
                <LIGHT_STATUS>Priv</LIGHT_STATUS>
            </LIGHT_INFORMATION>
            <LIGHT_INFORMATION>
                <LIGHT_COLOUR>R</LIGHT_COLOUR>  <-- LIGHT_COLOUR R
                <LIGHT_HEIGHT>6</LIGHT_HEIGHT>
                <LIGHT_RANGE>3</LIGHT_RANGE>    <-- LIGHT_RANGE 3
                <LIGHT_STATUS>Priv</LIGHT_STATUS>
            </LIGHT_INFORMATION>
        </LIGHT_INFORMATION_LIST>
    </NTC_LIGHTLISTPRODUCT>    
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>  <--- Second Group
    <NTC_LIGHTLISTPRODUCT>
        <IMMUTABLE_ID>5586</IMMUTABLE_ID>
        <INTERNATIONAL_LIGHT_NUMBER>E1324</INTERNATIONAL_LIGHT_NUMBER>
        <VICINITY>LERICI</VICINITY>
        <LIGHT_INFORMATION_LIST>
            <LIGHT_INFORMATION>
                <LIGHT_COLOUR>G</LIGHT_COLOUR>   <-- LIGHT_COLOUR G
                <LIGHT_HEIGHT>7</LIGHT_HEIGHT>
                <LIGHT_RANGE>3</LIGHT_RANGE>     <-- LIGHT_RANGE 3
                <LIGHT_STATUS>Priv</LIGHT_STATUS>
            </LIGHT_INFORMATION>
            <LIGHT_INFORMATION>
                <LIGHT_COLOUR>G</LIGHT_COLOUR>   <-- LIGHT_COLOUR G
                <LIGHT_HEIGHT>6</LIGHT_HEIGHT>
                <LIGHT_RANGE>3</LIGHT_RANGE>     <-- LIGHT_RANGE 3
                <LIGHT_STATUS>Priv</LIGHT_STATUS>
            </LIGHT_INFORMATION>
        </LIGHT_INFORMATION_LIST>
    </NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>   <--- Third Group
    <NTC_LIGHTLISTPRODUCT>
        <IMMUTABLE_ID>5574</IMMUTABLE_ID>
        <INTERNATIONAL_LIGHT_NUMBER>E1328.5</INTERNATIONAL_LIGHT_NUMBER>
        <VICINITY>MARINA DI CARRARA</VICINITY>
        <LIGHT_INFORMATION_LIST>
            <LIGHT_INFORMATION>
              <LIGHT_COLOUR>W-G</LIGHT_COLOUR> <-- Contains'-', go to the next
              <LIGHT_SIGNAL_GROUP>()</LIGHT_SIGNAL_GROUP>
              <LIGHT_HEIGHT>32</LIGHT_HEIGHT>
              <LIGHT_RANGE>12</LIGHT_RANGE>    
              <LIGHT_STATUS>Priv</LIGHT_STATUS>
        </LIGHT_INFORMATION>
        <LIGHT_INFORMATION>
              <LIGHT_COLOUR>R</LIGHT_COLOUR>  <-- LIGHT_COLOUR R
              <LIGHT_HEIGHT>32</LIGHT_HEIGHT>
              <LIGHT_RANGE>10</LIGHT_RANGE>   <-- LIGHT_RANGE 10
              <LIGHT_STATUS>Priv</LIGHT_STATUS>
        </LIGHT_INFORMATION>
        <LIGHT_INFORMATION>
              <LIGHT_COLOUR>G</LIGHT_COLOUR>  <-- LIGHT_COLOUR G
              <LIGHT_HEIGHT>32</LIGHT_HEIGHT>
              <LIGHT_RANGE>10</LIGHT_RANGE>   <-- LIGHT_RANGE 10
              <LIGHT_STATUS>Priv</LIGHT_STATUS>
        </LIGHT_INFORMATION>
        <LIGHT_INFORMATION>
              <LIGHT_COLOUR>W</LIGHT_COLOUR> <-- LIGHT_COLOUR W
              <LIGHT_HEIGHT>32</LIGHT_HEIGHT>
              <LIGHT_RANGE>12</LIGHT_RANGE>  <-- LIGHT_RANGE 12
              <LIGHT_STATUS>Priv</LIGHT_STATUS>
        </LIGHT_INFORMATION>
    </LIGHT_INFORMATION_LIST>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>

这是我的xslt:

<td style="text-align:center; width:0.99cm; color:red; padding-left:3; padding-right:3" valign="top">
        <xsl:for-each select="LIGHT_INFORMATION_LIST/LIGHT_INFORMATION/LIGHT_RANGE[not(contains(preceding-sibling::LIGHT_COLOUR, '-'))]">

            <!-- Prendo le lettere dei colori in LIGHT_COLOUR-->
            <xsl:variable name="LCorrente" select="preceding-sibling::LIGHT_COLOUR"/>                                                                                                                           
            <xsl:variable name="LPrecedente" select="preceding::LIGHT_RANGE[1][not(contains(preceding-sibling::LIGHT_COLOUR, '-'))][preceding::IMMUTABLE_ID = $EF]/preceding-sibling::LIGHT_COLOUR"/>

            <!-- Prendo i numeri di portata in LIGHT_RANGE-->
            <xsl:variable name="NCorrente" select="number(.)"/>
            <xsl:variable name="NPrecedente" select="number(preceding::LIGHT_RANGE[1][not(contains(preceding-sibling::LIGHT_COLOUR, '-'))][preceding::IMMUTABLE_ID=$EF])"/>

            <xsl:if test="not($LPrecedente) or $LPrecedente != $LCorrente" >
                <xsl:value-of select="$LCorrente"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="$NCorrente"/>
                <br/>                                                                   
            </xsl:if>

        </xsl:for-each>
</td>

这是输出:

enter image description here 要做出的更正如下:

enter image description here

我解释说:

在第一种情况下:
所有元素的LIGHT_RANGE等于3(或其他数字)在这种情况下并不重要LIGHT_COLOUR在输出中我会&#39; 3&#39 ;;

在第二种情况下:
LIGHT_COLOUR和LIGHT_RANGE是一样的,写&#39; 3&#39;;

在第三种情况下:
如果Light_COLOUR包含&#39; - &#39;去下一个。如果至少有一个LIGHT_RANGE不同,则必须考虑每个LIGHT_RANGE的特性,并获得输出:R 10 - G 10和W 12

1 个答案:

答案 0 :(得分:0)

<xsl:choose>
    <xsl:when test="count(LIGHT_INFORMATION_LIST//LIGHT_RANGE) = count(LIGHT_INFORMATION_LIST//LIGHT_RANGE[. = (current()//LIGHT_RANGE)[1]])='false'">
        <xsl:for-each select="LIGHT_INFORMATION_LIST/LIGHT_INFORMATION/LIGHT_RANGE[not(. = preceding::LIGHT_RANGE[preceding::IMMUTABLE_ID=$EF])]">
            <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
        <xsl:for-each select="LIGHT_INFORMATION_LIST/LIGHT_INFORMATION/LIGHT_RANGE[not(contains(preceding-sibling::LIGHT_COLOUR, '-'))]">
        <!-- Prendo le lettere dei colori in LIGHT_COLOUR-->
        <xsl:variable name="LCorrente" select="preceding-sibling::LIGHT_COLOUR"/>                                                                                                                           
        <xsl:variable name="LPrecedente" select="preceding::LIGHT_RANGE[1][not(contains(preceding-sibling::LIGHT_COLOUR, '-'))][preceding::IMMUTABLE_ID = $EF]/preceding-sibling::LIGHT_COLOUR"/>

        <!-- Prendo i numeri di portata in LIGHT_RANGE-->
        <xsl:variable name="NCorrente" select="number(.)"/>
        <xsl:variable name="NPrecedente" select="number(preceding::LIGHT_RANGE[1][not(contains(preceding-sibling::LIGHT_COLOUR, '-'))][preceding::IMMUTABLE_ID=$EF])"/>

        <xsl:if test="not($LPrecedente) or $LPrecedente != $LCorrente" >
            <xsl:value-of select="$LCorrente"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="$NCorrente"/>
            <br/>                                                                   
        </xsl:if>
        </xsl:for-each>
    </xsl:otherwise>
</xsl:choose>