【问题标题】:How can I add namespaces to the root element of my XML using XSLT?如何使用 XSLT 将名称空间添加到我的 XML 的根元素?
【发布时间】:2013-04-05 13:53:17
【问题描述】:

我有一个输入 XML

<Request>
  <Info>
    <Country>US</Country>
    <Part>A</Part>
   </Info>
</Request>

我的输出应该是这样的

<Request
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://hgkl.kj.com">
  <Info>
    <Country>US</Country>
    <Part>A</Part>
  </Info>
</Request>

请告诉我如何添加多个命名空间和一个默认命名空间,如上述 XML。

【问题讨论】:

    标签: xml xslt xml-parsing


    【解决方案1】:

    这就是我在 XSLT 2.0 中的做法...

    XML 输入

    <Request>
        <Info>
            <Country>US</Country>
            <Part>A</Part>
        </Info>
    </Request>
    

    XSLT 2.0

    <xsl:stylesheet version="2.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="*" priority="1">
            <xsl:element name="{local-name()}" namespace="http://hgkl.kj.com">
                <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
                <xsl:namespace name="xsd" select="'http://www.w3.org/2001/XMLSchema'"/>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:element>
        </xsl:template>
    
    </xsl:stylesheet>
    

    XML 输出

    <Request xmlns="http://hgkl.kj.com"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <Info>
          <Country>US</Country>
          <Part>A</Part>
       </Info>
    </Request>
    

    这是一个 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="@*|text()|comment()|processing-instruction()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="/Request">
            <Request xmlns="http://hgkl.kj.com" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <xsl:apply-templates select="@*|node()"/>           
            </Request>
        </xsl:template>
    
        <xsl:template match="*">
            <xsl:element name="{local-name()}" namespace="http://hgkl.kj.com">
                <xsl:apply-templates select="@*|node()"/>
            </xsl:element>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多