简体   繁体   English

如何使用XSLT将顶级元素添加到XML?

[英]How to add top level element to XML using XSLT?

I have a simple XML, that I want to add a new root to. 我有一个简单的XML,我想添加一个新的根。 The current root is <myFields> and I want to add <myTable> so it would look like. 当前的根是<myFields> ,我想添加<myTable>所以它看起来像。

<myTable>
    <myFields>
    .
    .
    </myFields>
</myTable>

Something like this should work for you... 这样的事情对你有用......

<xsl:template match="/">
  <myTable>
    <xsl:apply-templates/>
  </myTable>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

This is probably the shortest solution :) : 这可能是最短的解决方案 :):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
        <myTable> 
            <xsl:copy-of select="node()" /> 
        </myTable> 
    </xsl:template> 
</xsl:stylesheet>

This stylesheet: 这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/*">
        <myTable>
            <xsl:call-template name="identity"/>
        </myTable>
    </xsl:template>
    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Note : Copy everything ( also PIs before root element ), and add myTable befere root element . 注意 :复制所有内容( 也是根元素之前的PI ),并添加myTable befere 根元素

you helped get me close enough 你让我足够近

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:element name="myTable">
            <xsl:copy-of select="*" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM