Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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在变量中压缩整个xml_Xslt - Fatal编程技术网

使用xslt在变量中压缩整个xml

使用xslt在变量中压缩整个xml,xslt,Xslt,如何复制变量中的整个xml 下面是示例xml: <?xml version="1.0" encoding="UTF-8"?> <products author="Jesper"> <product id="p1"> <name>Delta</name> <price>800</price> <stock

如何复制变量中的整个xml

下面是示例xml:

  <?xml version="1.0" encoding="UTF-8"?>
      <products author="Jesper">
         <product id="p1">
            <name>Delta</name>
            <price>800</price>
            <stock>4</stock>
         </product>
      </products>

三角洲
800
4.
我尝试过xslt,但它不起作用

     <?xml version="1.0" encoding="UTF-8"?>
        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="@*|node()">
            <xsl:variable name="reqMsg">
                <xsl:copy>
                   <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
           </xsl:variable>
       <xsl:copy-of select="$reqMsg"/>
    </xsl:template>
   </xsl:stylesheet>

问候,,
Rahul

您的转换失败,因为在某一点上,它试图创建一个包含属性节点的变量(结果树片段)。这是不允许的

现在还不清楚“将整个XML复制到变量”是什么意思。但您可能只想在根节点上使用
select
属性:

<xsl:variable name="reqMsg" select="/"/>

这将实际创建包含文档根节点的节点集的变量。将此变量与
xsl:copy of
一起使用将输出整个文档。


<xsl:copy-of select="document('path/to/file.xml')" />
或者,如果您不止一次需要它,为了避免重复文档名称:

<xsl:variable name="filepath" select="'path/to/file.xml'" />
…
<xsl:copy-of select="document($filepath)" />

document()
的结果应该缓存在IIRC中,所以不要担心重复调用它。

您在这里的目的是什么?XML文档仍然存在,为什么需要将其复制到变量中?在编写样式表时,不一定知道XML源文件的路径。那么,将其设置为