Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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
Xml 在XSLT2.0中定义变量时使用xsl:copy of_Xml_Variables_Xslt - Fatal编程技术网

Xml 在XSLT2.0中定义变量时使用xsl:copy of

Xml 在XSLT2.0中定义变量时使用xsl:copy of,xml,variables,xslt,Xml,Variables,Xslt,我(在一个虚构的简化世界中)有一个基本的源XML结构: <?xml version="1.0"?> <library> <book> <language>french</language> <title>Les Misérables</title> </book> <book> <language>english</

我(在一个虚构的简化世界中)有一个基本的源XML结构:

<?xml version="1.0"?>
<library>
  <book>
    <language>french</language>
    <title>Les Misérables</title>
  </book>
  <book>
    <language>english</language>
    <title>Great Expectations</title>
  </book>
</library>

法语
拉伯斯酒店
英语
远大的期望
我使用xsl:variable的select属性定义了一个变量myVar1

<xsl:variable name="myVar1" select="library/book[language='french']"/>
    <xsl:variable name="myVar2">
        <xsl:copy-of select="library/book[language='french']"/>
    </xsl:variable>

我定义了一个变量myVar2,在xsl:variable中包含一个xsl:copy

<xsl:variable name="myVar1" select="library/book[language='french']"/>
    <xsl:variable name="myVar2">
        <xsl:copy-of select="library/book[language='french']"/>
    </xsl:variable>

我希望myVar1和myVar2是相同的,但事实并非如此

    <xsl:value-of select="$myVar1/title"/> <!--result: Les Misérables-->
    <xsl:value-of select="$myVar2/title"/> <!--empty result-->

定义myVar2时有什么问题?如何表明我希望它是一个结构化的可查询变量(如myVar1)?如果我有几个条件语句来定义变量,使用select是不方便的

这是完整的XSLT文件

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">

    <xsl:variable name="myVar1" select="library/book[language='french']"/>

    <xsl:variable name="myVar2">
      <xsl:copy-of select="library/book[language='french']"/>
    </xsl:variable>

    <xsl:value-of select="$myVar1/title"/>
    <xsl:value-of select="$myVar2/title"/>

  </xsl:template>
</xsl:stylesheet>

我测试了一下

如果您使用输入获取文件,您可以将其包含在

我希望myVar1和myVar2是相同的

但它们并不相同:

  • 第一个变量包含对中
    book
    节点的引用 源文件

  • 第二个变量在其自己的文档中包含
    book
    节点副本

因此,指示:

<xsl:value-of select="$myVar2/title"/>
你可以用

<xsl:variable name="myVar2" as="element()*">
  <xsl:copy-of select="library/book[language='french']"/>
</xsl:variable>

使变量保留一系列元素节点,而不是包含元素节点的文档(片段)节点。

中给出了规则


快速总结:如果没有
选择
属性,也没有
作为
属性,那么变量的值是一个新的文档节点,其中包含作为子元素复制的元素。正是这个额外的级别停止了路径表达式的工作。使用
select=“library/book”
时,变量的值是一个
book
元素,使用序列构造函数时,值是一个文档节点,它的子元素是
book
元素。

谢谢,但我的目标是将myVar2用作结构化元素。在输出中,我不需要entier结构,我想查询
$myVar2/title
,并仅显示这个title@user14027976我在awnser中添加了一行。这应该可以解决您的问题;)
<xsl:value-of select="$myVar2/title"/>
<xsl:value-of select="$myVar2/book/title"/>
<xsl:variable name="myVar2" as="element()*">
  <xsl:copy-of select="library/book[language='french']"/>
</xsl:variable>