我有一个简单的XML文档,想要添加一个新的根元素。当前的根元素是<myFields>
,我想要添加一个<myTable>
元素,使其看起来像这样。
<myTable>
<myFields>
.
.
</myFields>
</myTable>
类似下面这样的内容应该适合您:
<xsl:template match="/">
<myTable>
<xsl:apply-templates/>
</myTable>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
这可能是最短的解决方案 :)
<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>
这个样式表:
<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>
注意:复制所有内容(包括根元素之前的PIs),并在根元素之前添加myTable
。
你帮我接近了目标
<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>