[xsl] Unable to select wanted nodes

Subject: [xsl] Unable to select wanted nodes
From: "Mark" <mark@xxxxxxxxxxxx>
Date: Thu, 6 Oct 2011 13:02:14 -0700
For the below <Input> file, I want the <DesiredOutput> file but so far I have only been able to write code that produces <MyOutput>.

In <MyOutPut>:
(1) The mode="stamp" templates produce satisfactory <StampPage> elements. These templates and their output were included only for completeness and may be ignored.


(2) The mode="formats" templates produce the <FormatPage> and its child <StampButtons> properly, but I cannot puzzle out how to produce the children of <FormatButtons>. I am not even sure that what I have done so far is the best way to do this.

Side note: the redundant attribute in the <FormatPage> element appears solely as debugging aid.

Help graciously accepted,
Mark

<Input>
   <Set>
     <Stamp>
       <CatNumbers pofis-number="105"/>
       <Formats souvenir-sheet="105"/>
       <Formats se-tenant="105"/>
     </Stamp>
     <Stamp>
       <CatNumbers pofis-number="106"/>
       <Formats souvenir-sheet="105"/>
       <Formats se-tenant="105"/>
     </Stamp>
     <Stamp>
       <CatNumbers pofis-number="107"/>
       <Formats souvenir-sheet="105"/>
       <Formats se-tenant="107"/>
     </Stamp>
     <Stamp>
       <CatNumbers pofis-number="108"/>
       <Formats souvenir-sheet="105"/>
       <Formats se-tenant="107"/>
     </Stamp>
   </Set>
 </Input>
----------------
<DesiredOutput>
   <StampPage>
     <FormatButtons>
       <Formats state="active" souvenir-sheet="105"/>
       <Formats state="active" se-tenant="105"/>
     </FormatButtons>
     <StampButtons>
       <Stamp state="depressed" pofis-number="105"/>
     </StampButtons>
   </StampPage>
   <StampPage>
     <FormatButtons>
       <Formats state="active" souvenir-sheet="105"/>
       <Formats state="active" se-tenant="105"/>
     </FormatButtons>
     <StampButtons>
       <Stamp state="depressed" pofis-number="106"/>
     </StampButtons>
   </StampPage>
   <StampPage >
     <FormatButtons>
       <Formats state="active" souvenir-sheet="105"/>
       <Formats state="active" se-tenant="107"/>
     </FormatButtons>
     <StampButtons>
       <Stamp state="depressed" pofis-number="107"/>
     </StampButtons>
   </StampPage>
   <StampPage>
     <FormatButtons>
       <Formats state="active" souvenir-sheet="105"/>
       <Formats state="active" se-tenant="107"/>
     </FormatButtons>
     <StampButtons>
       <Stamp state="depressed" pofis-number="108"/>
     </StampButtons>
   </StampPage>
   <FormatPage souvenir-sheet="105">
     <FormatButtons>
       <Formats state="depressed" souvenir-sheet="105"/>
       <Formats label="1" state="active" se-tenant="105"/>
       <Formats label="2" state="active" se-tenant="107"/>
     </FormatButtons>
     <StampButtons>
       <Stamp label="1" state="active" pofis-number="105"/>
       <Stamp label="2" state="active" pofis-number="106"/>
       <Stamp label="3" state="active" pofis-number="107"/>
       <Stamp label="4" state="active" pofis-number="108"/>
     </StampButtons>
   </FormatPage>
   <FormatPage se-tenant="105">
     <FormatButtons>
       <Formats state="active" souvenir-sheet="105"/>
       <Formats label="1" state="depressed" se-tenant="105"/>
       <Formats label="2" state="active" se-tenant="107"/>
     </FormatButtons>
     <StampButtons>
       <Stamp label="1" state="active" pofis-number="105"/>
       <Stamp label="2" state="active" pofis-number="106"/>
     </StampButtons>
   </FormatPage>
   <FormatPage se-tenant="107">
     <FormatButtons>
       <Formats state="active" souvenir-sheet="105"/>
       <Formats label="1" state="active" se-tenant="105"/>
       <Formats label="2" state="depressed" se-tenant="107"/>
     </FormatButtons>
     <StampButtons>
       <Stamp label="1" state="active" pofis-number="107"/>
       <Stamp label="2" state="active" pofis-number="108"/>
     </StampButtons>
   </FormatPage>
 </DesiredOutput>

-------------------
<MyOutput>
 <StampPage>
   <FormatButtons>
     <Formats state="active" souvenir-sheet="105"/>
     <Formats state="active" se-tenant="105"/>
   </FormatButtons>
   <StampButtons>
     <Stamp state="depressed" pofis-number="105"/>
   </StampButtons>
 </StampPage>
 <StampPage>
   <FormatButtons>
     <Formats state="active" souvenir-sheet="105"/>
     <Formats state="active" se-tenant="105"/>
   </FormatButtons>
   <StampButtons>
     <Stamp state="depressed" pofis-number="106"/>
   </StampButtons>
 </StampPage>
 <StampPage>
   <FormatButtons>
     <Formats state="active" souvenir-sheet="105"/>
     <Formats state="active" se-tenant="107"/>
   </FormatButtons>
   <StampButtons>
     <Stamp state="depressed" pofis-number="107"/>
   </StampButtons>
 </StampPage>
 <StampPage>
   <FormatButtons>
     <Formats state="active" souvenir-sheet="105"/>
     <Formats state="active" se-tenant="107"/>
   </FormatButtons>
   <StampButtons>
     <Stamp state="depressed" pofis-number="108"/>
   </StampButtons>
 </StampPage>
 <FormatPage souvenir-sheet="105">
   <FormatButtons>
     <!-- My problem -->
   </FormatButtons>
   <StampButtons>
     <Stamp label="1" state="active" pofis-number="105"/>
     <Stamp label="2" state="active" pofis-number="106"/>
     <Stamp label="3" state="active" pofis-number="107"/>
     <Stamp label="4" state="active" pofis-number="108"/>
   </StampButtons>
 </FormatPage>
 <FormatPage se-tenant="105">
   <FormatButtons>
     <!-- My problem -->
   </FormatButtons>
   <StampButtons>
     <Stamp label="1" state="active" pofis-number="105"/>
     <Stamp label="2" state="active" pofis-number="106"/>
   </StampButtons>
 </FormatPage>
 <FormatPage se-tenant="107">
   <FormatButtons>
     <!-- My problem -->
   </FormatButtons>
   <StampButtons>
     <Stamp label="1" state="active" pofis-number="107"/>
     <Stamp label="2" state="active" pofis-number="108"/>
   </StampButtons>
 </FormatPage>
</MyOutput>

------------------------
Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"; exclude-result-prefixes="xs xd"
version="2.0">


<xsl:strip-space elements="*"/>

 <xsl:template match="Set">
   <xsl:element name="Output">
     <xsl:apply-templates mode="stamp"/>
     <xsl:apply-templates mode="formats"/>
   </xsl:element>
 </xsl:template>

<!-- Create a StampPage for every Stamp -->
<xsl:template match="Stamp" mode="stamp">
<xsl:element name="StampPage">
<xsl:element name="FormatButtons">
<xsl:apply-templates mode="stamp"/>
</xsl:element>
<xsl:element name="StampButtons">
<xsl:element name="Stamp">
<xsl:attribute name="state" select="'depressed'"/>
<xsl:attribute name="pofis-number" select="CatNumbers/@pofis-number"/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>


 <xsl:template match="Stamp" mode="formats">
   <xsl:apply-templates mode="formats"/>
 </xsl:template>

 <xsl:template match="Formats" mode="stamp">
   <xsl:element name="Formats">
     <xsl:attribute name="state" select="'active'"/>
     <xsl:copy-of select="@*"/>
   </xsl:element>
 </xsl:template>

<xsl:template match="Formats" mode="formats">
<xsl:if test="@* eq ../CatNumbers/@pofis-number">
<xsl:element name="FormatPage">
<xsl:copy-of select="@*"/>
<xsl:variable name="format" select="name(@*)"/>
<xsl:variable name="number" select="@*"/>
<xsl:element name="FormatButtons">
<!-- I do not know how to select the correct components -->
</xsl:element>
<xsl:element name="StampButtons">
<xsl:choose>
<xsl:when test="count(../../Stamp/Formats/@*[name(.)=$format]) eq 1">
<xsl:element name="Stamp">
<xsl:attribute name="state" select="'active'"/>
<xsl:copy-of select="../CatNumbers/@pofis-number"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:for-each-group select="../../Stamp/Formats" group-by="@*[name(.)=$format]">
<xsl:for-each select="current-group()">
<xsl:if test="@* eq $number">
<xsl:element name="Stamp">
<xsl:attribute name="label" select="position()"/>
<xsl:attribute name="state" select="'active'"/>
<xsl:copy-of select="../CatNumbers/@pofis-number"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:for-each-group>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:element>
</xsl:if>
</xsl:template>


</xsl:stylesheet>

Current Thread