简体   繁体   中英

how to remove namespace and retain only some of the elements from the original XML document using XSL?

Below is my XML. I wanted to parse this using XSL. What I want to achieve is to remove the namespace (xmlns) then just retain some of the elements and their attributes. I found a way to remove the namespace but when I put it together with the code to retain some of the elements, it doesn't work. I already tried the identity but still didn't work.

I hope someone out there could share something. Thank you very much in advance.

XML Input:

<Transaction xmlns="http://www.test.com/rdc.xsd">
    <Transaction>
       <StoreName id="aa">STORE A</StoreName>
       <TransNo>TXN0001</TransNo>
       <RegisterNo>REG001</RegisterNo>
       <Items>
          <Item id="1">
             <ItemID>A001</ItemID>
             <ItemDesc>Keychain</ItemDesc>
          </Item>
          <Item id="2">
             <ItemID>A002</ItemID>
             <ItemDesc>Wallet</ItemDesc>
          </Item>
       </Items>
       <IDONTLIKETHIS_1>
          <STOREXXX>XXX-</STOREXXX>
          <TRANSXXX>YYY</TRANSXXX>
       </IDONTLIKETHIS_1>
       <IDONTLIKETHIS_2>
          <STOREXXX>XXX-</STOREXXX>
          <TRANSXXX>YYY</TRANSXXX>
       </IDONTLIKETHIS_2>
    </Transaction>
<Transaction>

Expected XML Output:

<Transaction>
    <Transaction>
       <StoreName id="aa">STORE A</StoreName>
       <TransNo>TXN0001</TransNo>
       <RegisterNo>REG001</RegisterNo>
       <Items>
          <Item id="1">
             <ItemID>A001</ItemID>
             <ItemDesc>Keychain</ItemDesc>
          </Item>
          <Item id="2">
             <ItemID>A002</ItemID>
             <ItemDesc>Wallet</ItemDesc>
          </Item>
       </Items>
    </Transaction>
<Transaction>

Code used to remove the namespace (xmlns):

<xsl:template match="*">
   <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
   </xsl:element>
</xsl:template>

<xsl:template match="@*">
   <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
   </xsl:attribute>
</xsl:template>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:x="http://invia.fujitsu.com/RetailDATACenter/rdc.xsd">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>

     <xsl:template match="*">
      <xsl:element name="{name()}">
       <xsl:copy-of select="@*"/>
       <xsl:apply-templates select="node()"/>
      </xsl:element>
     </xsl:template>

     <xsl:template match="node()[not(self::*)]">
      <xsl:copy-of select="."/>
     </xsl:template>

     <xsl:template match="x:IDONTLIKETHIS_1 | x:IDONTLIKETHIS_2"/>
</xsl:stylesheet>

Explanation :

  1. The template matching "*" matches any element and recreates it ( <xsl:element> ) with the same name, but doesn't copy any namespace nodes. It also copies all attributes of this element. Then it applies templates (including itself -- recursively) on all of this element's children nodes -- not only elements but all types of children nodes: elements, text nodes, processing-instructions and comments.

  2. The last template matches any node we don't like to copy to the output and does exactly this (no copying ) with an empty template body.

  3. The second template matches and copies to the output all nodes that aren't elements, excluding the document node /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM