gpt4 book ai didi

xml - XSLT - 为一些 child 添加命名空间声明

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

出于某些原因,我需要更改现有的 xml 文件,并且我想使用 xslt 来执行此操作。我需要将一些命名空间声明从根节点移动到子节点。

基本上我从一个看起来像这样的文件开始:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://ns1" xmlns:ns2="http://ns2" xmlns:ns3="http://ns3">
<env:Header>
<ns1:parent11>

<ns1:child11>value</ns1:child11>
<ns1:child11>value</ns1:child11>
<ns1:child11>value</ns1:child11>

<ns1:child12>
<ns2:child21>value</ns2:child21>
<ns2:child22>value</ns2:child22>
</ns1:child12>

</ns1:parent11>
</env:Header>


<env:Body>
<ns3:parent31>
<ns3:child31>value</ns3:child31>
<ns3:child32>value</ns3:child32>

<ns3:child33>
<ns2:parent2>
<ns2:child23>value</ns2:child23>
<ns2:child23>value</ns2:child23>
<ns2:child23>value</ns2:child23>
</ns2:parent2>
</ns3:child33>

</ns3:parent31>
</env:Body>

我需要以这样的结尾:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://ns1" xmlns:ns2="http://ns2" xmlns:ns3="http://ns3">
<env:Header>
<parent11 xmlns="http://ns1">

<child11>value</child11>
<child11>value</child11>
<child11>value</child11>

<child12>
<ns2:child21>value</ns2:child21>
<ns2:child22>value</ns2:child22>
</child12>

</parent11>
</env:Header>


<env:Body>

<parent31 xmlns="http://ns3">
<child31>value</child31>
<child32>value</child32>

<child33>
<parent2 xmlns="http://ns2">
<child23>value</child23>
<child23>value</child23>
<child23>value</child23>
</parent2>
</child33>

</parent31>
</env:Body>

我是 xslt 的新手(我选择它是因为我认为这是正确的做法),所以我被困在一开始,我什至不知道如何开始。

最佳答案

解决方案 XSLT 1.0/XSLT 2.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://ns1"
xmlns:ns2="http://ns2"
xmlns:ns3="http://ns3"
version="1.0">

<xsl:template match="ns1:* | ns2:* | ns3:*">
<xsl:element name="{local-name()}" namespace="{namespace-uri()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

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

</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ns1"
xmlns:ns2="http://ns2" xmlns:ns3="http://ns3">
<env:Header>
<parent11 xmlns="http://ns1">

<child11>value</child11>
<child11>value</child11>
<child11>value</child11>

<child12>
<child21 xmlns="http://ns2">value</child21>
<child22 xmlns="http://ns2">value</child22>
</child12>

</parent11>
</env:Header>
<env:Body>
<parent31 xmlns="http://ns3">
<child31>value</child31>
<child32>value</child32>

<child33>
<parent2 xmlns="http://ns2">
<child23>value</child23>
<child23>value</child23>
<child23>value</child23>
</parent2>
</child33>

</parent31>
</env:Body>
</env:Envelope>

描述:

上层模板移除命名空间前缀——例如ns1 - 匹配元素。

第二个模板是所有其他元素、属性和文本的身份副本

XSLT-Processors 自行删除同一命名空间中子项的命名空间。

关于xml - XSLT - 为一些 child 添加命名空间声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39874718/

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