开发者

Automating exslt:node-set?

开发者 https://www.devze.com 2023-03-22 22:29 出处:网络
Not sure if this is possible, but trying set up something that doesn\'t make me have to type exslt:node-set when pull开发者_开发技巧ing values from a dynamically created node block. I am storing the e

Not sure if this is possible, but trying set up something that doesn't make me have to type exslt:node-set when pull开发者_开发技巧ing values from a dynamically created node block. I am storing the entire set of nodes in a variable, and wrapping it in exslt:node-set, but why does it not work when I then try to pull from it. Is this possible?

<xsl:variable name="LANG">
    <xsl:variable name="tmp">
        <xsl:element name="foo">
            <xsl:element name="bar">Hello</xsl:element>
        </xsl:element>
    </xsl:variable>
    <xsl:value-of select="exslt:node-set($tmp)"/>
</xsl:variable>


<!-- Love to be able to do this --> 
<xsl:value-of select="$LANG/foo/bar"/>

<!-- This does work --> 
<xsl:value-of select="exslt:node-set($LANG)/foo/bar"/>


In XSLT 1.0, the variable defined as in your example are called result tree fragments (RTF) and you can only use xsl:copy-of to copy the entire fragment to the result tree or xsl:value-of to copy the entire content. Example

 <xsl:copy-of select="$LANG"/>

If you want treat the variable as a temporary tree you need the node-set() extension.

The common way to deal with static tree fragments (like lookup tables) in XSLT 1.0 is to define them as children of the stylesheet root elements (using a custom namespace). Then you can use the document() function to retrieve the wanted value.

Note If you are using Saxon (v>6.5), you could simply set the stylesheet version to 1.1 and you will be able to manage the RTF without any node-set extension.


[XSLT 1.0]

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:empo="http://stackoverflow.com/users/253811/empo">

    <empo:LANG>
        <empo:foo>
            <empo:bar>Hello</empo:bar>
        </empo:foo>
    </empo:LANG>

    <xsl:template match="/">
        <xsl:variable name="LANG" select="document('')/*/empo:LANG"/>
        <xsl:value-of select="$LANG/empo:foo/empo:bar"/>
    </xsl:template>

</xsl:stylesheet>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号