示例 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xslnsv="http://sample2.1">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>    
</xsl:template>
<xsl:template match="//xslnsv:Activity">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:if test="not(@IsForCompensation) 
      and (./xslnsv:IsForCompensationSpecified)">
      <xsl:attribute name="IsForCompensation">
        <xsl:value-of 
          select="./xslnsv:IsForCompensationSpecified" />
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates
      select="@*|node()[local-name() 
        != 'IsForCompensationSpecified']" />
  </xsl:copy>
</xsl:template>    
</xsl:stylesheet>
这里我们有一个命名空间 xmlns:xslnsv="http://sample2.2" 当我们有一个具有相同命名空间的 xml 时它可以工作
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://sample2.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <ElementAtLevel1>
    <ElementAtLevel2 Id="cf9d2" Name="Pool 1">    
      <Activities>
        <Activity Id="ef84125a">          
          <IsForCompensationSpecified
            >false</IsForCompensationSpecified>
        </Activity>
        <Activity Id="39c5b8d8" Name="Task 1">
          <IsForCompensationSpecified 
            >true</IsForCompensationSpecified>
        </Activity>
      </Activities>
    </ElementAtLevel2>  
  </ElementAtLevel1>
  <ExtendedAttributes />
</Package>
产生输出为:
<?xml version="1.0"?>
<Package xmlns="http://sample2.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ElementAtLevel1>
    <ElementAtLevel2 Id="cf9d267d-e1ed-4616-adfb-d24d6844f775"
                     Name="Pool 1">    
      <Activities>
        <Activity Id="ef84125a-0a01-4d76-9b3b-413ffb3c7a74"    
                  IsForCompensation="false"/>
        <Activity Id="39c5b8d8-9a72-40d1-b3e4-8cd973ccdf03" 
                  Name="Task 1" 
                  IsForCompensation="true"/>
      </Activities>
    </ElementAtLevel2>  
  </ElementAtLevel1>
  <ExtendedAttributes/>
</Package>
但问题是:我们有一些具有不同命名空间的 xml,即http://sample2.1 具有不同命名空间的示例 xml
<?xml version="1.0"?>
<Package xmlns="http://sample2.1" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ElementAtLevel1>
    <ElementAtLevel2 Id="cf9d267d-e1ed-4616-adfb-d24d6844f775" 
                     Name="Pool 1">    
      <Activities>
        <Activity Id="ef84125a-0a01-4d76-9b3b-413ffb3c7a74" 
                  IsForCompensation="false"/>
        <Activity Id="39c5b8d8-9a72-40d1-b3e4-8cd973ccdf03" 
                  Name="Task 1" 
                  IsForCompensation="true"/>
      </Activities>
    </ElementAtLevel2>  
  </ElementAtLevel1>
  <ExtendedAttributes/>
</Package>
那么我们没有正确的输出。
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://sample2.1" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <ElementAtLevel1>
    <ElementAtLevel2 Id="cf9d2" Name="Pool 1">    
      <Activities>
        <Activity Id="ef84125a">
          <IsForCompensationSpecified
            >false</IsForCompensationSpecified>
        </Activity>
        <Activity Id="39c5b8d8" Name="Task 1">
          <IsForCompensationSpecified
            >true</IsForCompensationSpecified>
        </Activity>
      </Activities>
    </ElementAtLevel2>  
  </ElementAtLevel1>
  <ExtendedAttributes />
</Package>
我修改了 xslt 以动态更改名称空间。带有新更改的示例 xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xslnsv="http://sample2.2" >
  <xsl:output method="xml" indent="yes"/>
  <xsl:variable name="vUrl" select="'http://sample2.2'"/>
  <xsl:template match="*[namespace-uri()='http://sample2.1']">
    <xsl:element name="{name()}" namespace="{$vUrl}">
      <xsl:copy-of select="@*"/>
       <xsl:apply-templates/>
    </xsl:element>
  </xsl:template> 
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
 <xsl:template match="//xslnsv:Activity">
   <xsl:copy>
     <xsl:copy-of select="@*" />
     <xsl:if test="not(@IsForCompensation) 
                   and (./xslnsv:IsForCompensationSpecified)">
       <xsl:attribute name="IsForCompensation">
         <xsl:value-of 
           select="./xslnsv:IsForCompensationSpecified" />
       </xsl:attribute>
     </xsl:if>
     <xsl:apply-templates select="@*
       |node()[local-name() != 'IsForCompensationSpecified']" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
在我看来,它能够更改命名空间,但在更改命名空间后无法选择元素。可能是指来自源 xml 的旧命名空间,即 2.1
但我仍然没有得到正确的输出;我得到以下输出。
<?xml version="1.0"?>
 <Package xmlns="http://sample2.1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <ElementAtLevel1>
 <ElementAtLevel2 Id="cf9d2" Name="Pool 1">    
 <Activities>
 <Activity Id="ef84125a">          
 <IsForCompensationSpecified>false</IsForCompensationSpecified>
 </Activity>
 <Activity Id="39c5b8d8" Name="Task 1">
 <IsForCompensationSpecified>true</IsForCompensationSpecified>
 </Activity>
 </Activities>
 </ElementAtLevel2>  
 </ElementAtLevel1>
 <ExtendedAttributes/>
 </Package>