复制除根节点和属性XSLT之外的XML文件内容 [英] Copy XML file contents except for root node and attribute XSLT
问题描述
我正在处理一个小的XSLT文件,以复制XML文件的内容,并去除声明和根节点.根节点具有名称空间属性.
I am working on a small XSLT file to copy the contents of an XML file and strip out the declaration and root node. The root node has an namespace attribute.
我现在可以正常工作了,只是现在将名称空间属性复制到直接子节点上.
I currently have it working except for now the namespace attribute is now being copied to the direct children nodes.
到目前为止,这是我的xslt文件,没有什么大的或复杂的:
Here is my xslt file so far, nothing big or complicated:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:apply-templates select="node()" />
</xsl:template>
</xsl:stylesheet>
我的输入文件是这样的:
My input file is like this:
<?xml version="1.0" encoding="UTF-8"?>
<Report_Data xmlns="examplenamespace">
<Report_Entry>
<Report_Date>
</Report_Date>
</Report_Entry>
<Report_Entry>
<Report_Date>
</Report_Date>
</Report_Entry>
<Report_Entry>
<Report_Date>
</Report_Date>
</Report_Entry>
</Report_Data>
XSLT之后的输出如下:
The output after the XSLT is like this:
<Report_Entry xmlns="examplenamespace">
<Report_Date>
</Report_Date>
</Report_Entry>
<Report_Entry xmlns="examplenamespace">
<Report_Date>
</Report_Date>
</Report_Entry>
<Report_Entry xmlns="examplenamespace">
<Report_Date>
</Report_Date>
</Report_Entry>
问题是,每个Report_Entry标记现在都从我删除的根节点获取该xml名称空间属性.
The problem is, every Report_Entry tag is now getting that xml namespace attribute from the root node that I removed.
如果您想知道,我知道XSLT的输出格式不正确.稍后,在XSLT转换之后,我将添加XML声明和其他根节点名称.
In case you were wondering, I know the output of the XSLT is not well formed. I am adding the XML declaration and a different root node name later on after the XSLT trasnformation.
推荐答案
以下内容将产生所需的输出:
The following produces the output that you want:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8"/>
<!-- For each element, create a new element with the same local-name (no namespace) -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- Skip the root element, just process its children. -->
<xsl:template match="/*">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
这篇关于复制除根节点和属性XSLT之外的XML文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!