Reputation: 455
I'm working with XSLT. For one of my requirement I need to catch the tag <random-text:apple>/<random-text:colour>
The thing is my XSLT version is 1.0, so I don't have the full regex support.
Let me give a simple example to illustrate the problem statement:
sample.xml:
<Fruits>
<random-text:apple>
<random-text:colour>RED</random-text:colour>
<random-text:shape>ROUND</random-text:shape>
</random-text:apple>
<random-text:round-fruits>
<random-text:apple>
<random-text:colour>RED</random-text:colour>
</random-text:apple>
</random-text:round-fruits>
</Fruits>
So in the sample.xml, I want to catch <random-text:apple>/<random-text:colour>
tag but I want to avoid the same tag in other tree hierarchy like <random-text:round-fruits>/<random-text:apple>/<random-text:colour>
I tried something like this: "/*[contains(name(), 'apple')]/*[contains(name(), 'colour')]"
but as you can suspect, it is catching the /apple/colour element in all the tree hierarchy levels.
So, I want to ask:
UPDATE:
Sorry, I forgot to mention earlier that the random-text is properly defined but I have to do it for many such xml files, so I want something like: <any-namespace:apple>/<any-namespace:colour>
element
Upvotes: 0
Views: 175
Reputation: 116993
Perhaps I am missing the point here, because this seems like a trivial problem. The expression:
/Fruits/random-text:apple/random-text:colour
will select the random-text:colour
element in the random-text:apple
branch, and exclude the other random-text:colour
element in the random-text:round-fruits
branch.
This is assuming your XML has a proper namespace declaration for the random-text
prefix and that this declaration is repeated in your stylesheet.
Demo: https://xsltfiddle.liberty-development.net/asoTKq
-- added --
I have to do it for many such xml files, so I want something like: any-namespace:apple/any-namespace:colour element
That is not a healthy situation. Namespaces are part of the XML schema - and a stylesheet should be written for a specific schema.
Still, you could do:
/Fruits/*[local-name()='apple']/*[local-name()='colour']
Upvotes: 1