使用 XSLT 将根节点和子节点添加到根节点 XML 以及输出转换 xml

Add a root node and child to root node to XML using XSLT as well as output transformed xml

我想在一些 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 转换方面还很陌生,这是我的第一次尝试,我试图在它被消耗之前将其用作 editing/doctoring 一些 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>

像这样:

<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>

您可能想在第二个应用模板(在我的示例中为 second)上切换模式,并将匹配 /rootNode 的模板和此新模式中的任何后续内容放置。

编辑(根据您的评论)

从您的评论看来,您想要做的就是获取一些输入 XML 并在该输入周围添加两个元素 XML。最好的起点是使用恒等变换:

<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>

请注意,我删除了 <message>,因为它似乎已经在您输入的文档中,并且无论如何都会被复制。

另外,和我原来的回答一样,不会有匹配到/rootNode,除非你做一个微流水线,因为它不存在于你的原始输入中。上面的代码将起作用。如果您向其中添加具有更具体匹配项的任何模板,则可以覆盖该元素。如果您还想处理它的子项,请确保添加一个 xsl:apply-templates.

例如,假设您想将元素 foo 更改为 bar,您可以这样做(只需将模板添加到恒等变换中):

<xsl:template match="foo">
    <bar>
        <!-- process children, child text nodes, attributes etc -->
        <xsl:apply-templates select="node() | @*" />
    </bar>
</xsl:template>