简体   繁体   English

使用 XSL 转换表修改 XML 中的命名空间

[英]modify namespace in XML using XSL transformation sheet

I'm facing an issue while trying to change my XML namespace definition.我在尝试更改 XML 命名空间定义时遇到问题。

i have this namespace in my input xml : <ns3:DataElement xmlns:ns3="http://fakeurl_V3/xsd">我的输入 xml 中有这个命名空间: <ns3:DataElement xmlns:ns3="http://fakeurl_V3/xsd">

and lower in my xml i have a field : < productsList xsi:type="ns3:Segment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">在我的 xml 中,我有一个字段:< productsList xsi:type="ns3:Segment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

++++++++++++++++++++++++++++++++++++++++++++++++++ In my XSL i did this : ++++++++++++++++++++++++++++++++++++++++++++++++++在我的 XSL 中,我这样做了:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   
    xmlns:ns2="http://fakeurl_V2/xsd"
    xmlns:ns3="http://fakeurl_V3/xsd"
... others namespace ....
>

    <xsl:output method="xml" version="1.0" omit-xml-declaration="no"
        encoding="UTF-8" indent="yes" />
....

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

....

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++

there is the result :结果是:

<ns2:DataElement xmlns:ns2="http://fakeurl_V2/xsd"> <ns2:DataElement xmlns:ns2="http://fakeurl_V2/xsd">

<productsList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns3:Segment"> <productsList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns3:Segment">

As you can see the namespace ns2 has been changed as i wish But it did not affect this line : xsi:type="ns3:Segment" that i need to transform in : xsi:type="ns2:Segment"正如您所看到的,命名空间 ns2 已按照我的意愿进行了更改,但它并没有影响这一行:我需要转换的xsi:type="ns3:Segment"xsi:type="ns2:Segment"

Do you have any solution about that ?你有什么解决办法吗?

Thank you for your attention, i hope i made myself clear enougth.感谢您的关注,我希望我已经说清楚了。

Regards问候

You could add a template to replace the old prefix with the new one, such as:您可以添加一个模板来用新的前缀替换旧的前缀,例如:

<xsl:template match="@xsi:type">
    <xsl:attribute name="{name()}" select="replace(., '^ns3:', 'ns2:')" />
</xsl:template>

Demo : https://xsltfiddle.liberty-development.net/gVrvcxa演示https : //xsltfiddle.liberty-development.net/gVrvcxa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM