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

时间:2015-08-21 20:18:29

标签: 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>
    <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之前的解决方案。

1 个答案:

答案 0 :(得分:1)

您尝试实现的目标是所谓的(微)管道。无法输出结果文档并在单个转换中读回(由于语言的确定性原因),但很可能通过重新应用它来对中间输出进行转换: / p>

替换它:

<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>
相关问题