1

我需要更改以下 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 示例并进行了一些调整,但似乎没有任何效果。

谢谢!

4

2 回答 2

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>
于 2012-08-11T03:38:12.640 回答
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>

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

于 2012-08-10T22:55:27.673 回答