2014-10-06 88 views
1

我有要求將前綴添加到名稱空間。使用XSLT向名稱空間添加前綴

輸入XML:

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
     <ProductMovementReport Version="5.0" xmlns="urn:cidx:names:specification:ces:schema:all:5:0"> 
      <Header> 
       <ThisDocumentIdentifier> 
        <DocumentIdentifier>868</DocumentIdentifier> 
       </ThisDocumentIdentifier> 
      </Header> 

     </ProductMovementReport> 
    </soapenv:Body> 

我還需要更改版本=「‘5’」這我能順利拿到價值。

下面是慾望輸出。

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
     <urn1:ProductMovementReport Version="4.0" xmlns:urn1="urn:cidx:names:specification:ces:schema:all:4:0"> 
      <urn1:Header> 
       <urn1:ThisDocumentIdentifier> 
        <urn1:DocumentIdentifier>868</urn1:DocumentIdentifier> 
       </urn1:ThisDocumentIdentifier> 

      </urn1:Header> 

     </urn1:ProductMovementReport> 
    </soapenv:Body> 

我已經寫了這個代碼,除了名稱空間部分沒有變化,一切正常。我想我在模板匹配中缺少一些東西。

代碼:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="@*"> 
    <xsl:choose> 
     <xsl:when test="local-name() = 'Version' "> 
      <xsl:attribute name="{local-name()}"><xsl:value-of select="'4.0'"/></xsl:attribute> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
<xsl:template match="*"> 
    <xsl:element name="urn1:{local-name()}" namespace="urn:cidx:names:specification:ces:schema:all:4:0"> 
     <xsl:copy-of select="namespace::*"/> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 

這裏是萬一我錯過了什麼http://xsltransform.net/bdxtpP的鏈接。

除了命名空間部分,我非常接近解決它。

任何人都可以請指出我在哪裏做錯了嗎?

回答

0

您匹配的match="*"與所有元素匹配,無論名稱空間如何。嘗試通過測試命名空間URI來縮小範圍。

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="*[namespace-uri()='urn:cidx:names:specification:ces:schema:all:5:0']"> 
     <xsl:element name="urn1:{local-name()}" namespace="urn:cidx:names:specification:ces:schema:all:4:0"> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="@Version"> 
     <xsl:attribute name="{name()}"> 
      <xsl:text>4.0</xsl:text> 
     </xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <urn1:ProductMovementReport xmlns:urn1="urn:cidx:names:specification:ces:schema:all:4:0" Version="4.0"> 
     <urn1:Header> 
     <urn1:ThisDocumentIdentifier> 
      <urn1:DocumentIdentifier>868</urn1:DocumentIdentifier> 
     </urn1:ThisDocumentIdentifier> 
     </urn1:Header> 
    </urn1:ProductMovementReport> 
</soapenv:Body> 
+0

非常感謝你的工作現在:) – 2014-10-06 18:52:18