2014-10-06 106 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

相关问题