Home > Back-end >  Can we match a parent node using it's child element name?
Can we match a parent node using it's child element name?

Time:04-29

I want to match the parent node using it's child element name. Example:

<parent><child1>text</child1></parent>

Here i want to match parent element but i don't know the parent tag name but what i know is it's child element name. So i need to perform tranformation on parent tag and that's why i need to match the parent tag using it's child element name .And also if parent of child tag is root then i don't want to perform any transformation.

For more clearity i need to match something like <xsl:template match="[ancestor::contentblock]"> or <xsl:template match="[parent::contentblock]"> but xslt doesn't allow this.

CodePudding user response:

Try something like:

//*/*[child1]

If the xpath is used in an xsl:template match, you can leave off the //.

match="*/*[child1]"

Edit

This should be more of what you're looking for:

match="*[parent::*][.//child1]"

This matches any element that has a parent element and also a descendant child1 element.

Fiddle: http://xsltfiddle.liberty-development.net/93F8dVG

  • Related