检查属性等于value的节点是否存在

Hel*_*lge 10 xslt if-statement

我有以下XSLT变量:

<xsl:variable name="superid" select="/contentdata/id"/>
Run Code Online (Sandbox Code Playgroud)

此外,我有一个子节点的节点:

<nodes>
    <node name="foo" id="bar" />
    <node name="john" id="doe" />
    <node name="jane" id="tarzan" />
</nodes>
Run Code Online (Sandbox Code Playgroud)

现在,我想检查一个id属性等于superid的节点是否存在.

我尝试了以下(显然不起作用):

<xsl:if test="/nodes/node[@id = $superid]">Yes, Sir!</xsl:if>
Run Code Online (Sandbox Code Playgroud)

Dim*_*hev 15

用途:

boolean(/nodes/node/@id = $superid)
Run Code Online (Sandbox Code Playgroud)

这是一个完整的转换,显示了这一点:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vDoe" select="'doe'"/>
 <xsl:variable name="vXyz" select="'xyz'"/>

 <xsl:template match="/">
     id attribute with value "doe' exists: <xsl:text/>
     <xsl:value-of select="boolean(/nodes/node/@id = $vDoe)"/>
==========
     id attribute with value "xyz' exists: <xsl:text/>
     <xsl:value-of select="boolean(/nodes/node/@id = $vXyz)"/>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于提供的XML文档时:

<nodes>
    <node name="foo" id="bar" />
    <node name="john" id="doe" />
    <node name="jane" id="tarzan" />
</nodes>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

     id attribute with value "doe' exists: true
==========
     id attribute with value "xyz' exists: false
Run Code Online (Sandbox Code Playgroud)


Stu*_*tLC 5

你所拥有的似乎对我有用 (xsl 1.0) - 按照这里

我试图重新创建您的xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" indent="yes" />

    <xsl:template match="/xml">
        <xml>
            <xsl:apply-templates select="contentdata/id" />
        </xml>
    </xsl:template>

    <xsl:template match="id">
        <Result>
        <xsl:variable name="superid" select="."/>
        <SearchFor>
            <xsl:value-of select="$superid"/>
        </SearchFor>
        <IsPresent>
            <xsl:if test="/xml/nodes/node[@id = $superid]">Node is present</xsl:if>
        </IsPresent>
    </Result>
    </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

鉴于xml:

<xml>
    <contentdata>
        <id>doe</id>
        <id>unobtanium</id>
    </contentdata>
    <nodes>
        <node name='foo' id='bar' />
        <node name='john' id='doe' />
        <node name='jane' id='tarzan' />
    </nodes>
</xml>
Run Code Online (Sandbox Code Playgroud)

输出:

<xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Result>
        <SearchFor>doe</SearchFor>
        <IsPresent>Node is present</IsPresent>
    </Result>
    <Result>
        <SearchFor>unobtanium</SearchFor>
        <IsPresent />
    </Result>
</xml>
Run Code Online (Sandbox Code Playgroud)