Re: [xsl] Universally quantified test of child attribute presence/absence

Subject: Re: [xsl] Universally quantified test of child attribute presence/absence
From: Yves Forkl <Y.Forkl@xxxxxx>
Date: Tue, 20 Mar 2007 15:08:01 +0100
Abel Braaksma wrote:

Just a thought, based on what I think to grasp from what you want to do:

<!-- holds a sequence to two numbers, one with @my.. one with all-->
<xsl:variable name="mytest" select="count(*/@my_atrribute), count(*)" as="xs:integer+" />


<!-- choosing between the four possible scenarios. Note: order is important! -->
<xsl:choose>
<xsl:when test="$mytest[1] = $mytest[2]">
<!-- every child has @my_attribute -->
</xsl:when>
<xsl:when test="$mytest[1]">
<!-- some childs has @my_attribute -->
</xsl:when>
<xsl:when test="$mytest[2]">
<!-- no childs have @my_attribute -->
</xsl:when>
<xsl:otherwise>
<!-- no childs at all -->
</xsl:otherwise>
<xsl:choose>



As I said, just a thought, and perhaps way easier to write down and/or to understand. Like Larry Wall said: there's more than one way to do it.

Thank you for this suggestion, which due to its counting and list building technique however is much less natural to me than immediately testing for "universal" attribute presence / absence for my purposes.


I should perhaps have been clearer about the case distinction I am about to implement. I am facing exactly 3 cases (your third and fourth cases above get combined into my second case):

1) There are element children, and they all have the attribute.

2) Each of the element children, if any, does not have the attribute.

3) Some element children do have the attribute, some don't.

The solution I have found (thanks again to you and David) is to explicitly couple the existence of children with attribute presence for case 1, then proceed to case 2 where I just test for attribute absence before passing on to the "otherwise" clause representing case 3:

<xsl:choose>
  <xsl:when
    test="* and (every $child in * satisfies $child[@my_attribute])">
  </xsl:when>
  <xsl:when
    test="every $child in * satisfies $child[not(@my_attribute)]">
  </xsl:when>
  <xsl:otherwise>
  </xsl:otherwise>
<xsl:choose>

Relying on the evaluation order of the instructions within xsl:choose spares me from having to use more complicated (or less familiar :-)) test expressions.

Note that for case 2, testing revealed that I can't test for

not(every $child in * satisfies $child[@my_attribute])

instead as this fails when there is no child at all.


Yves


Current Thread