Jump to content

Xslt Parsing Unwanted Nodes


bakunin

Recommended Posts

XML

<ROOT><object><name>INPUT</name><type>element</type></object><object><name>TEXTAREA</name><type>element</type></object></ROOT>

XSLT

<xsl:template match="name"><xsl:value-of select="." /></xsl:template>

RESULTINPUTelementTEXTAREAelementIf i parse this XML with that XSLT, it will render my template but it will render an unparsed "element" under each object and evry other child node of the object. I believe i may parse the whole Object instead of just the child node "<name>" , what would be the right path so it would only parse the node "<name>". I don't want to parse "<element>" i want it to be hidden because i will use it with another completly different XSLT file.In other word its parsing the whole nodelist from the object and its displaying the content in text just under wich is quite frustrating, why would i want to parse it in text , what is the point of a template if it parse the text content of evry node under it.ps: Sorry for my bad english. :-/

Link to comment
Share on other sites

There are two ways of dealing with this. Choose whichever feels better: blacklisting the "type" element, or whitelisting the "name" element.If you plan to actually do something with the "type" element (or the "object" element for that matter), do it (and use apply-templates along the way to keep the hierarchy), and let XSLT handle the rest. If not, do some of the above two.Blacklisting - create a template that says nothing will happen for that element.

<xsl:template match="type"/>

If the XML was to later have other elements, THEY might get inserted at that spot. If the "object" element has some text in it (directly, not withing an element), it would display as well.Alternatively, blacklist all text() nodes, and handle text output manually, like so:

<xsl:template match="text()"/>

Whitelisting - use xsl:apply-templates on a higher level, explicitly stating the templates you want matched. Regardless of what other nodes the XML may contain, you'll only apply the templates you've specified

<xsl:template match="/"><xsl:apply-templates match="//name" /></xsl:template>

If your inner templates use <xsl:apply-templates/> too, you should be careful, as text nodes may still be outputted, unless blacklisted (at least at that level) of course.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...