使用xslt替换命名空间不会产生预期结果

时间:2013-08-10 12:51:11

标签: xslt

我希望将以下xml中的命名空间v1从http://choby.co.xx/DialogueServices/AM/V1替换为http://choby.co.xx/XYZ/WM/V1

Input
<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:v1="http://choby.co.xx/DialogueServices/AM/V1">
<soapenv:Header/>
<soapenv:Body>
  <v1:CancelAppointmentRequest>
     <Loc>1</Loc>
  </v1:CancelAppointmentRequest>
</soapenv:Body>
</soapenv:Envelope>

Expected:
<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:v1="http://choby.co.xx/XYZ/WM/V1">
<soapenv:Header/>
<soapenv:Body>
  <v1:CancelAppointmentRequest>
     <Loc>1</Loc>
  </v1:CancelAppointmentRequest>
</soapenv:Body>
</soapenv:Envelope>

xslt使用:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://choby.co.xx/XYZ/WM/V1"
xmlns:ns1old="http://choby.co.xx/DialogueServices/AM/V1">
<xsl:output omit-xml-declaration="no" />
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="/*">
    <xsl:element name="soapenv:{local-name()}"   namespace="http://www.w3.org/2003/05/soap-envelope">
        <xsl:apply-templates select="node()|@*" />
    </xsl:element>
</xsl:template>

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

Result:
<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
 <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
 <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <ns1:CancelAppointmentRequest xmlns:ns1="http://choby.co.xx/XYZ/WM/V1">
 <Loc>1</Loc>
 </ns1:CancelAppointmentRequest>
 </soapenv:Body>
 </soapenv:Envelope>

Header和Body命名空间与Envelope

不同

1 个答案:

答案 0 :(得分:0)

尝试这个怎么样:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://choby.co.xx/XYZ/WM/V1"
xmlns:ns1old="http://choby.co.xx/DialogueServices/AM/V1">

<xsl:output omit-xml-declaration="no" />

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

<xsl:template match="*[namespace-uri()='http://choby.co.xx/DialogueServices/AM/V1']">
    <xsl:element name="{local-name()}" namespace="http://choby.co.xx/XYZ/WM/V1">
        <xsl:apply-templates select="node()|@*" />
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

样式表会复制所有内容,但名称空间为http://choby.co.xx/DialogueServices/AM/V1的元素除外。这些元素被修改。