[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Matching or selecting template problem


Subject: RE: [xsl] Matching or selecting template problem
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 15 Jan 2009 16:00:42 -0000

> My problem:  I need to match all the following sibling 
> elements of the last <section> (in this case two <p> element 
> and a <ul>) and create a new sibling <section> with the 
> current following siblings as children of the new <section>.

This sounds as if you want to match "all the following siblings" jointly
rather than severally: that is, you want to write a template rule which
fires once to process the entire group of siblings. Well, you can't do that:
a template rule always matches a single node.

Why not just match the last section: match="section[last()]"?

You can then use <xsl:next-match/> to do the normal processing that would
apply to any other section, followed by the special logic for the last
section:

<xsl:template match="section[last()]">
  <xsl:next-match/>
  <section>
    <xsl:copy-of select="following-sibling::*"/>
  </section>
</xsl:template>

Alternatively, put the logic in the template rule for the parent element:

<xsl:template match="conbody">
  <xsl:apply-templates select="section"/>
  <section>
    <xsl:copy-of select="section[last()]/following-sibling::*"/>
  </section>
</xsl:template>

Michael Kay
http://www.saxonica.com/

> 
> I have tried:
> 
> conbody/section[last()]/following-sibling::*  (XPath returns 
> the correct result, but not valid as a template match)
> 
>   <xsl:template match="//conbody/section[last()]/p"/>  Again, 
> the XPath test gives me the expected result, but doesn't seem 
> to match in a tempate
>   <xsl:template match="//conbody/section[last()]/ul"/> Same 
> as previous.
> 
> I think I can create a template to match the following 
> siblings of the last <section>, I can use a For Each to 
> create the new section? Am I on the right track ? I'm unclear 
> as to how to create new high level elements in which I need 
> to nest EXISTING elements.
> 
> I appreciate any solutions, but I would also like to 
> understand the parts of this I seem to "get."
> 
> Current content:*
> *
> <?xml version="1.0" encoding="UTF-8"?>
> <concept  class="- topic/topic concept/concept">
>     <title>TITLE</title>
>     <prolog class="- topic/prolog "/>
>     <conbody class="- topic/body concept/conbody">
>         <section><title>TITLE</title></section>
>         <section><title>TITLE</title></section>
>         <section><title>TITLE</title></section>
>         <p>TEXT</p>
>         <p>TEXT</p>
>         <ul>
>             <li>
>                 <p>
>                     TEXT
>                 </p>
>             </li>
>             <li>
>                 <p>
>                     <xref/>
>                 </p>
>             </li>
>         </ul>
>     </conbody>
> </concept>
> 
> Desired Result:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <concept class="- topic/topic concept/concept">
>     <title>TITLE</title>
>     <prolog class="- topic/prolog "/>
>     <conbody class="- topic/body concept/conbody">
>         <section><title>TITLE</title></section>
>         <section><title>TITLE</title></section>
>         <section><title>TITLE</title></section>
>        <section>
>         <p>TEXT</p>
>         <p>TEXT</p>
>         <ul>
>             <li>
>                 <p>
>                     TEXT
>                 </p>
>             </li>
>             <li>
>                 <p>
>                     <xref/>
>                 </p>
>             </li>
>         </ul>
>        </section>
>     </conbody>
> </concept>
> 
> Charles Flanders
> cflanders@xxxxxxxxxx


Current Thread
Keywords