Reputation: 57
I have a parent xslt that is including multiple xslts. I have a need to remove a specific tag and its contents from the XML and the tag can be available anywhere.
Sample XML where I need to remove tag x:
<root>
<x>
content
</x>
<a>
<x>
content
</x>
retain
</a>
<b>
inside B
<c>
<x>
content
</x>
</c>
</b>
</root>
The simplest way to solve this is use an empty identity template :
<xsl:template match="//x"/>
But this works only if this is the only xslt working on the XML file. When I include other XSLTs into the parent XSLT, this template match works only at the first level. I want to understand why this is happening?
Also, is there any way to iterate through all children, when we don't know how deeply nested it might be in an XML using XSLT?
Thanks in advance!
Upvotes: 0
Views: 27
Reputation: 163645
It's likely to be either
(a) because there is another template rule matching the same elements with the same or higher precedence, or
(b) because the template rules in the other stylesheet modules never do an xsl:apply-templates
that selects the x element for processing. (Perhaps they do an xsl:copy-of
on an ancestor of the x element instead.)
You also ask:
Also, is there any way to iterate through all children, when we don't know how deeply nested it might be in an XML using XSLT?
I suspect that when you say "children" you mean "descendants" (getting the terminology right is key to understanding any information you read about XPath and XSLT).
The normal design pattern for XSLT is to process the tree recursively by using xsl:apply-templates
at each level to process the children of the current node, which achieves exactly the effect you are looking for.
Upvotes: 2