简体   繁体   中英

Apply templates that matchs two conditions

I need to list all the INST names but only if the "onlyTesters" node don´t exists in the "inst/idef" part of XML body above.

I know thats strange but I can´t change the XML I receive.

XML:

<river>
    <station num="699">
        <inst name="FLU(m)" num="1">
            <idef></idef>
        </inst>
        <inst name="Battery(V)" num="18">
            <idef>
                <onlyTesters/>
            </idef>
        </inst>
    </station>
    <INST name="PLU(mm)" num="0" hasData="1" virtual="0"/>
    <INST name="FLU(m)" num="1" hasData="1" virtual="0"/>
    <INST name="Q(m3/s)" num="3" hasData="1" virtual="1"/>
    <INST name="Battery(V)" num="18" hasData="1" virtual="0"/>
</river>

XSL:

<xsl:template match="/">
    <xsl:apply-templates select="//INST[@hasData = 1 and not(//inst[@num=(current()/@num)]/idef/onlyTesters)]/@name"/>
 </xsl:template>

<xsl:template match="//INST[@hasData = 1 and not(//inst[@num=(current()/@num)]/idef/onlyTesters)]/@name">
    <xsl:value-of select="@name"/>,
</xsl:template>

I´m having no match.

This is the result I expect:

PLU(mm),FLU(m),Q(m3/s)

You can achieve this with only one template:

<xsl:template match="/">
    <xsl:for-each select="//INST[@hasData='1' and not(@name=//inst[idef/onlyTesters]/@name)]">
        <xsl:value-of select="@name"/>
        <xsl:if test="position() != last()">, </xsl:if>
    </xsl:for-each>
</xsl:template>

Output is:

PLU(mm), FLU(m), Q(m3/s)

Cross-references are best resolved using a key - for example:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:key name="inst" match="inst" use="@name" />

<xsl:template match="/river">
    <xsl:for-each select="INST[@hasData = 1 and not(key('inst', @name)/idef/onlyTesters)]">
        <xsl:value-of select="@name"/>
        <xsl:if test="position() != last()">,</xsl:if>
    </xsl:for-each>
</xsl:template> 

</xsl:stylesheet>

Or even simpler:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:key name="exclude" match="onlyTesters" use="ancestor::inst/@name" />

<xsl:template match="/river">
    <xsl:for-each select="INST[@hasData = 1 and not(key('exclude', @name))]">
        <xsl:value-of select="@name"/>
        <xsl:if test="position() != last()">, </xsl:if>
    </xsl:for-each>
</xsl:template> 

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM