首页 文章

使用一个XSLT删除命名空间和添加属性时出现问题

提问于
浏览
0

我是XSLT的新手,在尝试删除名称空间并使用一个XSLT添加一些属性时,我需要一些指导 . 请注意,我能够在没有使用两个不同的XSLT的情况下执行这些操作 - 但是每当我尝试将这两个XSLT结合起来时,命名空间就会再次出现 . 我哪里错了?我已经尝试使用“XSLT to remove namespace and add attribute to a tag”作为参考而没有任何成功 .

我的示例输入(这只是一个示例输入 - 我的想法是有一个xslt可以使用任何XML输入,即1.Names空格应该从整个XML中删除,2.attributes必须添加到所有节点):

XSLT版本是1.0

<MESSAGE xmlns="http://www.mismo.org/residential/2009/schemas" 
    xmlns:plus="http://www.csp.com/Securitization/csp-uldd-plus">
  <ABOUT_VERSIONS>
    <ABOUT_VERSION>
        <AboutVersionIdentifier>XXX 1.0</AboutVersionIdentifier>
        <CreatedDatetime>2015-08-24T15:25:10</CreatedDatetime>
    </ABOUT_VERSION>
  </ABOUT_VERSIONS>
  <EXTENSION>
    <plus:LoanInd1>false</plus:LoanInd1>
    <plus:LoanInd2>3.5</plus:LoanInd2>
    <plus:LoanInd3>true</plus:LoanInd3>
  </EXTENSION>
</MESSAGE>

用于删除名称空间的XSLT:

xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 <!-- keep comments -->
 <xsl:template match="comment()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*">
    <!-- remove element prefix -->
    <xsl:element name="{local-name()}">
      <!-- process attributes -->
      <xsl:for-each select="@*">
        <!-- remove attribute prefix -->
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

用于添加属性的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!--- -This is Identity Template which copies the input-->
<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

<!--- -This is the template which acts on the elements to Add Sequence-->
        <xsl:template match="node()">
                <xsl:copy>
                        <xsl:variable name="index"><xsl:number level="multiple" format="1"/></xsl:variable>
                        <xsl:attribute name="dtfkeyseq"><xsl:value-of select="$index"/></xsl:attribute>
                        <xsl:apply-templates select="node()|@*"/>
                </xsl:copy>
        </xsl:template>
</xsl:stylesheet>

所需输出:

<?xml version="1.0" encoding="UTF-8"?>
<MESSAGE dtfkeyseq="1">
    <ABOUT_VERSIONS dtfkeyseq="1">
        <ABOUT_VERSION dtfkeyseq="1">
            <AboutVersionIdentifier dtfkeyseq="1">XXX 1.0</AboutVersionIdentifier>
            <CreatedDatetime dtfkeyseq="1">2015-08-24T15:25:10</CreatedDatetime>
        </ABOUT_VERSION>
    </ABOUT_VERSIONS>
    <EXTENSION dtfkeyseq="1">
        <LoanInd1 dtfkeyseq="1">false</plus:LoanInd1>
        <LoanInd2 dtfkeyseq="1">3.5</plus:LoanInd2>
        <LoanInd3 dtfkeyseq="1">true</plus:LoanInd3>
    </EXTENSION>
</MESSAGE>

1 回答

  • 0

    看看这是否适合您:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- remove namespace from elements -->
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*"/>
            <!-- add attribute -->
            <xsl:attribute name="dtfkeyseq">
                <xsl:number level="multiple"/>
            </xsl:attribute>    
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    
    <!-- remove namespace from attributes -->
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
    
    <!-- copy comments -->
    <xsl:template match="comment()">
        <xsl:copy/>
    </xsl:template>
    
    </xsl:stylesheet>
    

相关问题