Resolving subelements of a variable that has been passed into a template

馋奶兔 提交于 2019-12-11 19:39:35

问题


data.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="myxslt2.xslt"?>
<data>
    <foo>Hello</foo>
    <bar>World</bar>
    <foobar>This is a test</foobar>
</data>

myxslt2.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" version="1.0">
    <xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes"/>
    <xsl:variable name="main" select="/data"/>
    <xsl:template name="myTemplate">
        <xsl:param name="myparam"/>
        Inner1:<xsl:value-of select="msxsl:node-set($myparam)"/>
        <br/>
        Inner2:<xsl:value-of select="msxsl:node-set($myparam)/foobar"/>
    </xsl:template>
    <xsl:template match="/data">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        HTML STARTS
            <br/>
            <xsl:variable name="data" select="."/>
            Outer1:<xsl:value-of select="$data"/>
            <br/>
            Outer2:<xsl:value-of select="$data/foobar"/>
            <br/>
            <xsl:call-template name="myTemplate">
                <xsl:with-param name="myparam">
                    <xsl:value-of select="$data"/>
                </xsl:with-param>
            </xsl:call-template>
        </html>
    </xsl:template>
</xsl:stylesheet>

Output:

HTML STARTS 
Outer1: Hello World This is a test 
Outer2:This is a test
Inner1: Hello World This is a test 
Inner2:

Can someone explain why the inner doesn't resolve the subelement while the outer does?


回答1:


The problem lies with when you pass your the $data variable as a parameter to myTemplate

 <xsl:call-template name="myTemplate">
    <xsl:with-param name="myparam">
        <xsl:value-of select="$data"/>
    </xsl:with-param>
 </xsl:call-template>

Because you are using xsl:value-of here, this is only passing the text value of the nodes held in $data; i.e. the parameter is just a single text node. To retain the nodes you would need to use xsl:copy-of

 <xsl:call-template name="myTemplate">
    <xsl:with-param name="myparam">
      <xsl:copy-of select="$data"/>
    </xsl:with-param>
 </xsl:call-template>

Strictly speaking, this passing a "result tree fragment" which is why you would have to use the node-set extension function, but you would have to modify your use of it because the data node is not strictly the parent node here, the document fragment is

    Inner1:<xsl:value-of select="msxsl:node-set($myparam)/data"/>
    <br />
    Inner2:<xsl:value-of select="msxsl:node-set($myparam)/data/foobar"/>

But, you don't actually have to use node-set here at all really. Change how call the template to this...

<xsl:call-template name="myTemplate">
   <xsl:with-param name="myparam" select="$data"/>
</xsl:call-template>

There is an important difference here. This is no longer a result tree fragment, but is referencing the input document directly, meaning you don't have to use node-set at all.

<xsl:template name="myTemplate">
    <xsl:param name="myparam"/>
    Inner1:<xsl:value-of select="$myparam"/>
    <br />
    Inner2:<xsl:value-of select="$myparam/foobar"/>
</xsl:template>


来源:https://stackoverflow.com/questions/19284037/resolving-subelements-of-a-variable-that-has-been-passed-into-a-template

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