gpt4 book ai didi

xml - 将命名空间前缀添加到根节点

转载 作者:行者123 更新时间:2023-12-04 17:04:31 28 4
gpt4 key购买 nike

我需要更改根节点的命名空间并将命名空间前缀添加到仅根元素而不是子元素。

我有以下 XML:

<?xml version="1.0" encoding="UTF-8"?> 
<Class xmlns="https://api.ladbrokes.com/v1/sportsbook-couchbase/SportsbookCouchbase.xsd">
<blurb >Test</blurb>
<channels >
<e >I</e>
<e >J</e>
<e >K</e>
</channels>
<classSortCode >Test</classSortCode>
<classStatus >Test</classStatus>
<creationDateTime >2013-03-21T22:29:01.58+05:30</creationDateTime>
<isActive >true</isActive>
<lastUpdatedDateTime >2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
<locale >Test</locale>
</Class>

我需要这个成为
<?xml version="1.0" encoding="UTF-8"?> 
<ns0:Class xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd">
<blurb >Test</blurb>
<channels >
<e >I</e>
<e >J</e>
<e >K</e>
</channels>
<classSortCode >Test</classSortCode>
<classStatus >Test</classStatus>
<creationDateTime >2013-03-21T22:29:01.58+05:30</creationDateTime>
<isActive >true</isActive>
<lastUpdatedDateTime >2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
<locale >Test</locale>
</ns0:Class>

我可以使用 XSLT 实现这一点吗?请在这方面帮助我。

谢谢,
湿婆

最佳答案

这是您如何为文档元素提供不同的命名空间并将所有其他元素移动到空命名空间中的方法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

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

<xsl:template match="/*">
<xsl:element name="ns0:{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

<xsl:template match="*/*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>

在您的示例输入上运行时,结果是:
<ns0:Class xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd">
<blurb>Test</blurb>
<channels>
<e>I</e>
<e>J</e>
<e>K</e>
</channels>
<classSortCode>Test</classSortCode>
<classStatus>Test</classStatus>
<creationDateTime>2013-03-21T22:29:01.58+05:30</creationDateTime>
<isActive>true</isActive>
<lastUpdatedDateTime>2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
<locale>Test</locale>
</ns0:Class>

为了澄清起见,这就是您如何更改文档元素的命名空间并将其他所有内容保留在它们已有的命名空间中的方法。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

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

<xsl:template match="/*">
<xsl:element name="ns0:{local-name()}">
<xsl:copy-of select="namespace::*" />
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>

这就是结果。请注意顶部命名空间声明中微小但关键的区别:
<ns0:Class xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd" 
xmlns="https://api.ladbrokes.com/v1/sportsbook-couchbase/SportsbookCouchbase.xsd">
<blurb>Test</blurb>
<channels>
<e>I</e>
<e>J</e>
<e>K</e>
</channels>
<classSortCode>Test</classSortCode>
<classStatus>Test</classStatus>
<creationDateTime>2013-03-21T22:29:01.58+05:30</creationDateTime>
<isActive>true</isActive>
<lastUpdatedDateTime>2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
<locale>Test</locale>
</ns0:Class>

关于xml - 将命名空间前缀添加到根节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15554587/

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