在根元素中更改xml命名空间

时间:2012-08-10 19:02:56

标签: xml xslt

我需要更改以下xml的命名空间

<?xml version="1.0" encoding="UTF-8"?>
<cli:IWResource xmlns:cli="namespace_1">
    <Name>MFCflowWithAdapter</Name>
    <Type>process</Type>
    <File>MFCflowWithAdapter.iwp</File>
    <Description/>
    <GUID>4C0BF41F-CB37-4815-B93F-1B6C2A103C6A</GUID>
    <targetServer>6.1.5</targetServer>
    <ImportedFrom>
        <ServerURI>http://maria-ubuntu:9000</ServerURI>
        <UserId>test</UserId>
        <Password>ENCR(3157318131043128321832252993249)</Password>
        <ImportDate>2012-07-26T16:22:52.644-04:00</ImportDate>
    </ImportedFrom>
    <Dependencies>
        <Dependency>
            <Name>RDBMS_oracle</Name>
            <Type>adapter</Type>
            <GUID>ACACF542E270035D7311E584E63F256C</GUID>
            <Path>/aa</Path>
            <UsageCount>1</UsageCount>
        </Dependency>
    </Dependencies>
</cli:IWResource>

对此:

<cli:IWResource xmlns:cli="namespace_2">

我尝试了各种XSLT样本并进行了一些调整,但似乎没有任何效果。

谢谢!

2 个答案:

答案 0 :(得分:1)

此转化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:old="namespace_1" xmlns:new="namespace_2" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vOldNS" select=
 "document('')/*/namespace::old"/>

 <xsl:variable name="vNewNS" select=
 "document('')/*/namespace::new"/>

 <xsl:template match="*">
  <xsl:choose>
   <xsl:when test="namespace-uri()=$vOldNS">
     <xsl:element name="{name()}" namespace="{$vNewNS}">
       <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
   </xsl:when>
   <xsl:otherwise>
     <xsl:element name="{name()}" namespace="{namespace-uri()}">
       <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<cli:IWResource xmlns:cli="namespace_1">
    <Name>MFCflowWithAdapter</Name>
    <Type>process</Type>
    <File>MFCflowWithAdapter.iwp</File>
    <Description/>
    <GUID>4C0BF41F-CB37-4815-B93F-1B6C2A103C6A</GUID>
    <targetServer>6.1.5</targetServer>
    <ImportedFrom>
        <ServerURI>http://maria-ubuntu:9000</ServerURI>
        <UserId>test</UserId>
        <Password>ENCR(3157318131043128321832252993249)</Password>
        <ImportDate>2012-07-26T16:22:52.644-04:00</ImportDate>
    </ImportedFrom>
    <Dependencies>
        <Dependency>
            <Name>RDBMS_oracle</Name>
            <Type>adapter</Type>
            <GUID>ACACF542E270035D7311E584E63F256C</GUID>
            <Path>/aa</Path>
            <UsageCount>1</UsageCount>
        </Dependency>
    </Dependencies>
</cli:IWResource>

生成想要的正确结果

<cli:IWResource xmlns:cli="namespace_2">
   <Name>MFCflowWithAdapter</Name>
   <Type>process</Type>
   <File>MFCflowWithAdapter.iwp</File>
   <Description/>
   <GUID>4C0BF41F-CB37-4815-B93F-1B6C2A103C6A</GUID>
   <targetServer>6.1.5</targetServer>
   <ImportedFrom>
      <ServerURI>http://maria-ubuntu:9000</ServerURI>
      <UserId>test</UserId>
      <Password>ENCR(3157318131043128321832252993249)</Password>
      <ImportDate>2012-07-26T16:22:52.644-04:00</ImportDate>
   </ImportedFrom>
   <Dependencies>
      <Dependency>
         <Name>RDBMS_oracle</Name>
         <Type>adapter</Type>
         <GUID>ACACF542E270035D7311E584E63F256C</GUID>
         <Path>/aa</Path>
         <UsageCount>1</UsageCount>
      </Dependency>
   </Dependencies>
</cli:IWResource>

答案 1 :(得分:0)

这是根元素特有的:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:old="namespace_1"
xmlns:cli="namespace_2"
exclude-result-prefixes="old">

<xsl:template match="old:IWResource">
    <cli:IWResource>
        <xsl:apply-templates />
    </cli:IWResource>
</xsl:template>

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

</xsl:stylesheet>

或者您是否需要适用于不同根元素甚至儿童的通用解决方案?