1

How can one remove all text, but leave the structure intact?

for example:

<animals>
  <animal id="1">
    <type>cat</type>
    <food>
      <fav>miauwmjam</fav>
      <quantity unit="day">50g</quantity>
    </food>
  </animal>
</animals>

transformed into

<animals>
  <animal id="">
    <type></type>
    <food>
      <fav></fav>
      <quantity unit=""></quantity>
    </food>
  </animal>
</animals>

so also the attribute vales are empty...

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
eddy147
  • 4,853
  • 8
  • 38
  • 57

1 Answers1

5
<?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" indent="yes"/>

  <!-- copy all nodes -->
  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- clear attributes -->
  <xsl:template match="@*">
    <xsl:attribute name="{name()}" />
  </xsl:template>

  <!-- ignore text content of nodex -->
  <xsl:template match="text()" />

</xsl:stylesheet>
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316