Home > Blockchain >  How to remove type attribute from XML using XSLT1.0
How to remove type attribute from XML using XSLT1.0

Time:05-10

Assume that I have an XML as below.

<CustomerOrder>
   <CustomerId type="string">1000</CustomerId>
   <CustomerName type="string">Johnny</CustomerName>
   <Age type="Number">20</Age>
</CustomerOrder>

I need to write and XSL transformer so that I can remove the type attribute and the final output should be something like below.

<CustomerOrder>
   <CustomerId>1000</CustomerId>
   <CustomerName>Johnny</CustomerName>
   <Age>20</Age>
</CustomerOrder>

Any ideas??

CodePudding user response:

Just use this stylesheet realizing what was suggested in the first comment:

<?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" omit-xml-declaration="yes" indent="yes"/>

  <!-- identity template - copies everything as it is except for other rules matching -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>  
  
  <!-- Match all type attributes and ignore them -->
  <xsl:template match="@type" />

</xsl:stylesheet>
  • Related