0

我需要列出所有 INST 名称,但前提是“onlyTesters”节点不存在于上述 XML 正文的“inst/idef”部分中。

我知道这很奇怪,但我无法更改收到的 XML。

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>

我没有对手。

这是我期望的结果:

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

2 回答 2

1

您只需一个模板即可实现此目的:

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

输出是:

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

于 2019-08-20T22:01:32.497 回答
0

交叉引用最好使用一个来解决- 例如:

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>

或者更简单:

<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>
于 2019-08-20T22:09:00.110 回答