Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 将模板应用于变量/序列中的元素_Xslt_Xpath_Xslt 2.0_Xpath 2.0 - Fatal编程技术网

Xslt 将模板应用于变量/序列中的元素

Xslt 将模板应用于变量/序列中的元素,xslt,xpath,xslt-2.0,xpath-2.0,Xslt,Xpath,Xslt 2.0,Xpath 2.0,当我使用“apply templates”并选择一个变量序列时,模板是作用于序列中元素的上下文,还是作用于文档中元素的上下文 在下面的例子中,它似乎要么可以,要么没有,但我不明白为什么 <root> <a/> <b/> <c><a/></c> <a/> </root> <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet ve

当我使用“apply templates”并选择一个变量序列时,模板是作用于序列中元素的上下文,还是作用于文档中元素的上下文

在下面的例子中,它似乎要么可以,要么没有,但我不明白为什么

<root>
<a/>
<b/>
<c><a/></c>
<a/>
</root>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="root">
    <!--this variable give me a sequence of "a" elements-->
    <xsl:variable name="someElementA1" select="//a"/>
    <xsl:variable name="someElementA2">
        <xsl:copy-of select="//a"/>
    </xsl:variable>
    <xsl:for-each select="$someElementA1/a">
        <xsl:element name="test">
            <xsl:text>This is scenario 1: </xsl:text>
            <xsl:apply-templates select="."/>
        </xsl:element>
    </xsl:for-each>
    <xsl:for-each select="$someElementA2/a">
        <xsl:element name="test">
            <xsl:text>This is scenario 2: </xsl:text>
            <xsl:apply-templates select="."/>
        </xsl:element>
    </xsl:for-each>
    <xsl:element name="test">
        <xsl:text>This is scenario 3: </xsl:text>
        <xsl:apply-templates select="$someElementA1"/>
    </xsl:element>
    <xsl:element name="test">
        <xsl:text>This is scenario 4: </xsl:text>
        <xsl:apply-templates select="$someElementA2/a"/>
    </xsl:element>
</xsl:template>
<!--these are the templates to apply--> 
<xsl:template match="a[parent::c]">
    <xsl:text>This element has a parent</xsl:text>
</xsl:template>
<xsl:template match="a[parent::root]">
    <xsl:text>This element is in the root element</xsl:text>
</xsl:template>
<xsl:template match="a">
    <xsl:text>This element is in the sequence</xsl:text>
</xsl:template>
</xsl:stylesheet>

这是场景1:
这是场景2:
这是场景3:
这是场景4:
此元素有一个父元素
此元素位于根元素中
此元素在序列中
输出:

<test>This is scenario 2: This element is in the sequence</test>
<test>This is scenario 2: This element is in the sequence</test>
<test>This is scenario 2: This element is in the sequence</test>
<test>This is scenario 3: This element is in the root elementThis element has a parentThis element is in the root element</test>
<test>This is scenario 4: This element is in the sequenceThis element is in the sequenceThis element is in the sequence</test>
这是场景2:该元素在序列中
这是场景2:该元素在序列中
这是场景2:该元素在序列中
这是场景3:此元素位于根元素中此元素具有父元素此元素位于根元素中
这是场景4:此元素在序列中此元素在序列中此元素在序列中

您得到的结果是由以下事实造成的:

  • 一个节点序列包含的节点与其各自文档中的节点相同,而不是节点的副本

  • 创建由
    select
    属性中的表达式选择的每个节点的副本

  • 创建一个新的XML片段(临时树),这是变量
    $someElementA2

    $someElementA1/a
    
    不选择任何节点,因为
    $someElementA1
    中的元素都没有名为
    a
    的子元素

    $someElementA2/a
    

    选择文档节点(
    /
    )的子元素
    a

    好问题(+1)。请参阅我的答案以获得详细的解释。我做了进一步的研究。。。而不是使用xsl:sequence的副本。但这似乎不起作用,除非您还将as=“element()*”添加到变量中。最重要的是元素集合和元素序列之间存在差异,我想这更像是在文档上下文中导航元素路径并对其进行操作。@Darin Walsh:是的,如果变量体中生成了元素,它有一个默认类型
    document-node()
    。但你为什么要这么做呢?最好使用直接的定义:
    记住——千万不要在
    的正文中放东西。请始终尝试在变量的
    select
    属性中指定变量的值。我之所以将其放在变量体中,是因为我使用了复杂的条件,或者是将元素按顺序添加在一起。我将查看变量是否能够处理实际文档节点和生成元素的这种“混合”内容,以及当它通过参数传递时,序列会发生什么变化。可能有一种方法可以通过使用“for…”的select语句来执行上述操作@Darin Walsh:这将引发一个错误:
    ,因为在变量体内部您引用的是同一个变量,但该变量尚未定义。。。