【问题标题】:Add a root node and child to root node to XML using XSLT as well as output transformed xml使用 XSLT 以及输出转换的 xml 将根节点和子节点添加到 XML
【发布时间】:2015-08-21 20:18:29
【问题描述】:

我正在尝试将两个节点添加到某些 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”和“子消息”并输出结果的任何想法?

<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 的解决方案。

【问题讨论】:

  • 我是否正确,您尝试在单个转换中:1)将第一个转换输出到外部 XML 文件(并用 2 个额外元素包围您的文档)2)将此 XML 文件转换为别的 ?我问是因为我看到您创建了rootNode(= 它不在您的源 XML 中)并且有一个与之匹配的模板。
  • @ColinMaudry 输出文件只是为了看看我的转换是否有效。在我的实际场景中,我只需将两个外部节点添加到 xml。我将在我的问题中添加一个预期的 xml 块

标签: xml xslt xslt-2.0


【解决方案1】:

看起来您想要实现的是所谓的(微)管道。不可能输出结果文档并在单个转换中将其读回(出于语言确定性的原因),但很可能通过重新应用中间输出对中间输出进行转换:

替换这个:

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

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

另外,和我原来的回答一样,/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>

【讨论】:

  • 我可能应该说清楚,但我只想将 xml 输出到一个文件,以便我可以测试我的更改并根据需要进行调整。我的主要目标只是通过添加问题中所示的两个外部节点来转换 xml。我已经更新了问题以显示我对 xml 的期望。
  • @Popo:我更新了答案以向您展示这样做:用一些元素包围输入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 2013-02-17
相关资源
最近更新 更多