Re: [xsl] ancestor/subsequent descendant test

Subject: Re: [xsl] ancestor/subsequent descendant test
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 27 Mar 2009 17:14:53 +0100
At 2009-03-28 04:51 +1300, Trevor Nicholls wrote:
My input documents are allowed to contain nested sections. An optional
attribute marks out certain sections as significant. I want to detect a
situation in which a section which contains a descendant significant section
does not contain a subsequent INsignificant section (other than descendants
of any significant sections). It's complicated because the nested sections
are not (necessarily) immediate descendants of each other.

I'm afraid I cannot readily understand what you want, but I can focus on your choice of wording to make an attempt at an answer.


if the section it is wrapped in did not have the significant attribute set then
I want to report it, because it is INsignificant and a descendant of a
section which contains a prior significant section.

So you want descendants of sections without the attribute that have a preceding sibling with the attribute.


I'm a bit stuck with this template, which identifies the correct ancestor,

Why the ancestor? Above you want to report the wrapped section, not the wrapping section.


It surely isn't as complex as I'm finding it, can anybody help please?

It might be because of the complexity of describing it.


<xsl:template match="section[@sig="Y"]>

I don't think you want to focus on a section *with* significance, as I interpret you want to report the descendants with the problem.


I hope the code below helps.

. . . . . . . . . . . Ken

T:\>type trevor1.xml
 <section>
   <include>
     <section>.A.</section>
     <section sig="Y">
       <section>.B.</section>
       <include>
         <section>.C.</section>
       </include>
     </section>
   <!-- NB -->
     <section sig="Y">
       <section>.D.</section>
     </section>
   </include>
 </section>
T:\>xslt2 trevor1.xml trevor.xsl

T:\>type trevor2.xml
 <section>
   <include>
     <section>.A.</section>
     <section sig="Y">
       <section>.B.</section>
       <include>
         <section>.C.</section>
       </include>
     </section>
   <!-- NB -->
     <section>
       <section>.D.</section>
     </section>
   </include>
 </section>
T:\>xslt2 trevor2.xml trevor.xsl
Found a problem section: /section[1]/include[1]/section[3]/section[1]
T:\>type trevor.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:output method="text"/>

<!--the section is a descendant of a section without significance that
    has a preceding sibling with significance-->
<xsl:template match="section[not(@sig='Y')]
                            [preceding-sibling::section[@sig='Y']]//
                     section">
  <xsl:text>Found a problem section: </xsl:text>
  <!--report the XPath context-->
  <xsl:for-each select="ancestor-or-self::*">
    <xsl:value-of select="concat('/',name(.),'[',
                        count(preceding-sibling::*[node-name(.)=
                                                   node-name(current())])+1,
                        ']')"/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="/|*">
  <xsl:apply-templates select="*"/>
</xsl:template>

</xsl:stylesheet>
T:\>

--
XSLT/XSL-FO/XQuery training in Los Angeles (New dates!) 2009-06-08
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread