Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transform only the specified elements, dropping the rest

I am trying to develop an XSLT stylesheet which will transform an xml into another by keeping in view:

  1. By default the stylesheet should display nothing for each element (not even the text).
  2. If there is an explicit template match for an element, then copy that element, it's attributes and all its sub-elements (and their attributes).

In other words, identity transform only the elements explicitly specified.


1 Answers

Well, to do nothing for most, surely something like:

<xsl:template match="/*">
  <xsl:copy>
    <xsl:apply-templates select="*"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="*">
    <xsl:apply-templates select="*"/>
</xsl:template>

Then add matches for what you do want:

<xsl:template match="Foo | Bar">
    <xsl:copy-of select="."/>
</xsl:template>

However, it seems like a very unusual requirement. Normally you simply use matches that navigate to the known content through knowledge of the xml.

like image 147
Marc Gravell Avatar answered Oct 26 '25 12:10

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!