Vlax
Vlax

Reputation: 1527

How to find if element has ancestor based on a variable or parameter (XPath 2.0)

The problem I'm trying to solve is to get all elements that contain only a unique text element but at the same time also being able to exclude node trees based on a element name passed as a parameter to the XSLT sheet.

The part to select all the nodes containing only a unique text element that's not empty was relatively easy:

$StartNode//element()/text()[normalize-space(.)]/parent::*

where $StartNode is a Xpath Expression to a specific Element in the XML document.

Now the part I had quite some some problems with, excluding an specific node tree from the results:

so I was hoping for something like:

$StartNode//element()/text()[normalize-space(.)]/parent::*[not(ancestor::**$element_to_be_excluded**)]

where $element_to_be_excluded is the element name to exclude.

But it's not possible to use a variable on that part of the expression...

So I came up with this solution

<xsl:variable name="ancestors_of_current_node" as="xs:string*">
   <xsl:sequence select="$current_parent_node/ancestor::*/name()"/>
</xsl:variable>
<xsl:variable name="is_value_in_sequence" as="xs:boolean" select="functx:is-value-in-sequence($exclude_node_local_name, $ancestors_of_current_node)"/>
<xsl:if test="not($is_value_in_sequence)">
    <xsl:call-template name="dataElementMap">
       <xsl:with-param name="node_item" select="$current_parent_node"/>
    </xsl:call-template>
</xsl:if>

where functx:is-value-in-sequence is:

<xsl:function name="functx:is-value-in-sequence" as="xs:boolean" xmlns:functx="http://www.functx.com">
  <xsl:param name="value" as="xs:anyAtomicType?"/>
  <xsl:param name="seq" as="xs:anyAtomicType*"/>
  <xsl:sequence select="$value = $seq"/>
</xsl:function>

Now the question is, it's there a more elegant way to solve this problem?

Thanks in advance for any hint.

Regards Vlax

Upvotes: 1

Views: 3547

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

If $element_to_be_excluded is just the element's name, use:

$StartNode//element()
             [text()[normalize-space(.)]]
                [not(ancestor::*/name() = $element_to_be_excluded)]

If $element_to_be_excluded is a sequence of one or more elements, use:

$StartNode//element()
             [text()[normalize-space(.)]]
                [not(ancestor::* intersect $element_to_be_excluded)]

Explanation:

  1. The annecessary parent::* is eliminated.

  2. If the element name is given, we add an additinal predicate that is true() only when the set of names of the ancestors of the selected element doesn't contain the specific name contained in $element_to_be_excluded.

  3. If $element_to_be_excluded contains a sequence of one or more elements, we add an additional predicate that is true() only if the selected element isn't one of the elements contained in $element_to_be_excluded.

Do note that in both cases a single XPath expression can be used to select the wanted elements -- it isn't necessary to use any XSLT instructions at all.

Upvotes: 3

Martin Honnen
Martin Honnen

Reputation: 167591

I would always write stuff like <xsl:variable name="foo"><xsl:sequence select="someExpression"/></xsl:variable> as <xsl:variable name="foo" select="someExpression"/>. I am not good at making suggestions without seeing an input sample but I suspect instead of using a named template and those two variables and the xsl:if wrapping a call-template the code could be written using a template in a mode e.g.

<xsl:apply-templates select=".[not(ancestor::*[local-name() eq $exclude_node_local_name])]" mode="m1"/>

with your named template dataElementMap being replaced by e.g.

<xsl:template match="*" mode="m1">...</xsl:template>

(or perhaps some more specialized match pattern).

Upvotes: 2

Tim C
Tim C

Reputation: 70618

Instead of doing (which doesn't work due to the variable, as you mention)

$StartNode//element()/text()[normalize-space(.)]
   /parent::*[not(ancestor::$element_to_be_excluded)]

Could you try this instead, to check the ancestor's name?

$StartNode//element()/text()[normalize-space(.)]
   /parent::*[not(ancestor::*[local-name() = $element_to_be_excluded])]

Upvotes: 3

Related Questions