XSLT define a variable and check if it exists after

99封情书 提交于 2019-12-10 11:09:50

问题


I am attempting to transform an XML document. First, I am defining a global variable:

 <xsl:variable name="foo"><xsl:value-of select="bar"/></xsl:variable>

Now there is a chance that the XML I'm transforming has <bar>some data</bar> defined. There is also a chance that it is not defined.

Once I declare the global variable below, I'm trying to output the following if it is defined:

<foo>DEFINED</foo>

and if it's not defined:

<foo>NOT DEFINED</foo>

I am using <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

What's the best way of going about this?


回答1:


Since you're using XSLT 1.0, you could use string() to do the test.

Here's an example stylesheet:

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

    <xsl:template match="/*">
        <xsl:variable name="foo" select="bar"/>
        <results>
            <xsl:choose>
                <xsl:when test="string($foo)">
                    <foo>DEFINED</foo>
                </xsl:when>
                <xsl:otherwise>
                    <foo>NOT DEFINED</foo>
                </xsl:otherwise>
            </xsl:choose>           
        </results>
    </xsl:template>

</xsl:stylesheet>

Note that white-space is collapsed so <bar> </bar> will return false. Also, string() will work when testing an element directly instead of a variable.

Here's some input/output examples:


Input

<test>
    <bar/>
</test>

or

<test>
    <bar></bar>
</test>

or

<test>
    <bar>    </bar>
</test>

Output

<foo>NOT DEFINED</foo>

Input

<test>
    <bar>x</bar>
</test>

Output

<foo>DEFINED</foo>

If you could use XSLT 2.0, you could declare the variable as an xs:string and just use the variable name in the test (test="$foo").

Example:

<xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xs">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:variable name="foo" select="bar" as="xs:string"/>
        <results>
            <xsl:choose>
                <xsl:when test="$foo">
                    <foo>DEFINED</foo>
                </xsl:when>
                <xsl:otherwise>
                    <foo>NOT DEFINED</foo>
                </xsl:otherwise>
            </xsl:choose>           
        </results>
    </xsl:template>

</xsl:stylesheet>



回答2:


I'm not sure if this works but...

Add xmlns:fn="http://www.w3.org/2005/xpath-functions" to your stylesheet.

<xsl:choose>
  <xsl:when test="fn:boolean(foo)">
    <foo>DEFINED</foo>
  </xsl:when>
  <xsl:otherwise>
    <foo>NOT DEFINED</foo>
  </xsl:otherwise>
</xsl:choose>

Edit: @JimGarrison This will treat <bar/> or <bar></bar> as defined if it works.




回答3:


The variable foo will contain the xml node bar (not only the text which is content of bar) your xml contain bar. Therefore the answer from "@indeterminately sequenced" is right you can test if foo contains a xml node (bar) with test="boolean($foo)". But I would prefer 'test="name($foo)". Or test="name($foo) = 'bar' Which lead to:

<xsl:variable name ="foo" select="*/bar" />

<xsl:template match="/">
    <results>
        <xsl:choose>
            <xsl:when test="name($foo)">
                <foo>DEFINED</foo>
            </xsl:when>
            <xsl:otherwise>
                <foo>NOT DEFINED</foo>
            </xsl:otherwise>
        </xsl:choose>
    </results>
</xsl:template>


来源:https://stackoverflow.com/questions/16047919/xslt-define-a-variable-and-check-if-it-exists-after

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!