gpt4 book ai didi

xml - 复制除根节点和属性 XSLT 之外的 XML 文件内容

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:04 27 4
gpt4 key购买 nike

我正在处理一个小型 XSLT 文件以复制 XML 文件的内容并删除声明和根节点。根节点具有命名空间属性。

我目前让它正常工作,除了现在命名空间属性现在被复制到直接子节点。

到目前为止,这是我的 xslt 文件,没什么大的或复杂的:

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

我的输入文件是这样的:

<?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后的输出是这样的:

<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 命名空间属性。

如果您想知道,我知道 XSLT 的输出格式不正确。我将在 XSLT 转换之后添加 XML 声明和不同的根节点名称。

最佳答案

以下生成您想要的输出:

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

关于xml - 复制除根节点和属性 XSLT 之外的 XML 文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19733948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com