views:

149

answers:

1

Hi everyone, 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.

I hope that makes sense and should be very easy for most of you. Thanks in advance.

Regards, Ali

+2  A: 

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.

Marc Gravell
Thanks Marc, Your answer makes sense. Yes it is a strange requirement but what I am trying to do here is to control access to an xml document where the user will only be able to see the elements explicitly allowed by administrator. Users will see the file in raw XML format. Hope that makes sense. What I still need to do is to have the root element displayed in any case (in case of root element, do not display the child elements). Sorry forgot to mention this in original post. -- Ali
Thanksss a lott Marc. That is exactly what I wanted.