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
使用XSLT将根节点和根节点的子节点添加到XML,并输出转换后的XML_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

使用XSLT将根节点和根节点的子节点添加到XML,并输出转换后的XML

使用XSLT将根节点和根节点的子节点添加到XML,并输出转换后的XML,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我试图在一些xml的外部添加两个节点,并将转换后的结果输出到一个xml文件中。我一直没能让它工作。我的xml应该是: <message> <!-- ...other nodes and elements in here, not always consistent --> </message> (编辑)预期的XML: <?xml version="1.0" encoding="utf-8"?> <rootNode>

我试图在一些xml的外部添加两个节点,并将转换后的结果输出到一个xml文件中。我一直没能让它工作。我的xml应该是:

<message>
    <!-- ...other nodes and elements in here, not always consistent -->
</message>

(编辑)预期的XML:

<?xml version="1.0" encoding="utf-8"?>
<rootNode>
    <submessage>
        <message>
           <!-- ...other nodes and elements in here, not always consistent -->
        </message>
    </submessage>
</rootNode>

下面是我尝试过但似乎不起作用的xsl。关于如何添加两个节点“rootNode”和“submessage”并输出结果,我有什么想法吗

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    
    <xsl:output indent="yes" method="xml" />
    <xsl:variable name="root" select="/" />
    <xsl:variable name="filename" select="'testOutPut'" />

<xsl:template match="/">        
    <xsl:result-document href="{$filename}.xml" method="xml">
        <rootNode>
            <submessage>
                <message>
                    <xsl:apply-templates select="@*|node()"/>
                </message>
            </submessage>
        </rootNode>
    </xsl:result-document>
</xsl:template>

<xsl:template match="DefaultNodes/*">       
    <xsl:variable name="sourceNode" select="$root/message/*[name() = name(current())]" />
    <xsl:choose>            
        <xsl:when test="$sourceNode">
            <xsl:copy-of select="$sourceNode" />
        </xsl:when>            
        <xsl:otherwise>
            <xsl:copy-of select="." />
        </xsl:otherwise>            
    </xsl:choose>       
</xsl:template>

<xsl:template match="/rootNode">
    <Request xmlns="urn:NameSpace-Definition-Message" 
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">             
        <xsl:apply-templates select="@*|node()"/>           
    </Request>
</xsl:template>

</xsl:stylesheet>


编辑:另外,我对xsl转换非常熟悉,这是我的第一次尝试,我试图在xml被使用之前将其作为编辑/修改xml的解决方案。

您试图实现的是所谓的(微)管道。不可能输出一个结果文档并在一次转换中读回它(由于语言的决定论),但很有可能通过重新应用它对中间输出进行转换:

替换此项:

<xsl:template match="/">        
    <xsl:result-document href="{$filename}.xml" method="xml">
        <rootNode>
            <submessage>
                <message>
                    <xsl:apply-templates select="@*|node()"/>
                </message>
            </submessage>
        </rootNode>
    </xsl:result-document>
</xsl:template>
请注意,我删除了
,因为它似乎已经存在于您的输入文档中,并且将被复制

此外,与我的原始答案相同,除非您使用微管道,否则将不会与
/rootNode
匹配,因为它不存在于原始输入中。上述代码将起作用。如果向其中添加任何具有更特定匹配项的模板,则可以覆盖该元素。如果还希望处理其子级,请确保添加
xsl:apply templates

例如,假设要将元素
foo
更改为
bar
,可以这样做(只需将模板添加到标识转换中):



我是否正确地尝试了一次转换:1)将第一次转换输出到外部XML文件(并用两个额外的元素包围文档)2)将此XML文件转换为其他文件?我之所以问这个问题,是因为我看到您创建了
rootNode
(=它不在源XML中),并且有一个与之匹配的模板。@colinmary文档的输出只是为了看看我的转换是否有效。在我的实际场景中,我只需要将两个外部节点添加到xml中。我将在我的问题中添加一个预期的xml块。我可能应该说清楚,但我只想将xml输出到一个文件中,以便测试我的更改并根据需要进行调整。我的主要目标只是通过添加两个外部节点来转换xml,如问题所示。我已经更新了这个问题,以显示我对xml的期望。@Popo:我更新了答案,以向您展示如何做到这一点:用一些元素围绕输入。
<xsl:template match="/">        
    <xslvariable name="intermediate">
        <rootNode>
            <submessage>
                <message>
                    <xsl:apply-templates select="@*|node()"/>
                </message>
            </submessage>
        </rootNode>
    </xsl:variable>
    <xsl:result-document href="{$filename}.xml" method="xml">
        <xsl:apply-templates select="$intermediate/*" mode="second" />
    </xsl:result-document>
</xsl:template>
<xsl:template match="/">
    <!-- there is no need to use xsl:result-document if you only 
         want one output document, otherwise, add it back here -->
    <rootNode>
        <submessage>
            <xsl:apply-templates select="@*|node()"/>
        </submessage>
    </rootNode>
</xsl:template>

<!-- generically and recursively match and copy any node, incl. attribs -->
<xsl:template match="node()|@*">
    <xsl:copy>
        <!-- make sure to process any children -->
        <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
</xsl:template>
<xsl:template match="foo">
    <bar>
        <!-- process children, child text nodes, attributes etc -->
        <xsl:apply-templates select="node() | @*" />
    </bar>
</xsl:template>