【问题标题】:How to add namespace and prefix for all elements and attributes using XSLT?如何使用 XSLT 为所有元素和属性添加名称空间和前缀?
【发布时间】:2012-10-11 03:07:37
【问题描述】:

我的问题是如何使用 XSLT 为所有元素和属性添加名称空间和前缀? 我的输入 xml 原样....

<ProcessCreditMemo xmlns='CreditMemo' 
                   xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
                   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                   xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<ORDER_HEADERDetails>
    <ORDER_HEADER>
    <NAME>0010185214</NAME>

成为……

<ns0:ProcessCreditMemo xmlns='CreditMemo' 
                       xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
                       xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                       xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
                       xmlns:ns0="http://tempuri.org/">
<ns0:ORDER_HEADERDetails>
    <ns0:ORDER_HEADER>
   <ns0:NAME>0010185214</NAME>

我需要为所有元素和属性添加前缀“ns0:”,并在标题“ProcessCreditMemo”中添加命名空间“xmlns:ns0="http://tempuri.org/”。

我正在尝试构建一个 XSLT 来完成它...

<xsl:stylesheet version="1.0" 
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|text()|@*">
    <xsl:copy>
        <xsl:if test="local-name()='ProcessCreditMemo'">
            <xsl:attribute name="xmlns" namespace="http://tempuri.org/" />
        </xsl:if>

但生成的 XML 复制了前缀为空值。

<ProcessCreditMemo xmlns="CreditMemo" 
                   xmlns:ns0="http://tempuri.org/" 
                   xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                   ns0:xmlns="">

【问题讨论】:

    标签: xslt namespaces


    【解决方案1】:

    这种转变

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ns0="http://tempuri.org/">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*">
      <xsl:element name="ns0:{name()}" namespace="http://tempuri.org/">
       <xsl:copy-of select="namespace::*"/>
           <xsl:apply-templates select="node()|@*"/>
      </xsl:element>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于(更正)提供的输入(严重格式错误、不完整的 XML)

    <ProcessCreditMemo xmlns='CreditMemo'
      xmlns:xsd='http://www.w3.org/2001/XMLSchema'
      xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
      xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
      <ORDER_HEADERDetails>
        <ORDER_HEADER>
          <NAME>0010185214</NAME>
        </ORDER_HEADER>
      </ORDER_HEADERDetails>
    </ProcessCreditMemo>
    

    产生想要的、正确的结果(不是提供的严重畸形/不完整的想要结果):

    <ns0:ProcessCreditMemo xmlns:ns0="http://tempuri.org/" xmlns="CreditMemo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <ns0:ORDER_HEADERDetails>
          <ns0:ORDER_HEADER>
             <ns0:NAME>0010185214</ns0:NAME>
          </ns0:ORDER_HEADER>
       </ns0:ORDER_HEADERDetails>
    </ns0:ProcessCreditMemo>
    

    【讨论】:

    • 我在我的 xslt 中使用了类似的代码 sn-p 但我的 eclipse 一直将错误消息显示为无效的 xpath for .... .. 但是,xml 转换工作正常。
    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    相关资源
    最近更新 更多