Re: [xsl] Unable to select wanted nodes

Subject: Re: [xsl] Unable to select wanted nodes
From: "Mark" <mark@xxxxxxxxxxxx>
Date: Fri, 7 Oct 2011 09:26:19 -0700
Hi Brandon,
Your solution works wonderfully and was quite simple to render in the real code. Running it against almost 700 <Set>s flushed out two problems. One has to do with isolating formats to only stamps in which they occur, the other with label generation. I found no actual data that completely isolated the problem "Labels for different formats should be in different sequences; that is, each should start with 1". I can manufacture some if needed.


Attached is the original data, plus three additional <Sets>. I suspect Problem 1 is part and parcel of Problem 3, but included it in case my analysis was incorrect. The problems are identified in both the input and output data. In the stylesheet, I have removed the <StampPages> generating code because it worked perfectly.

Thanks again for all your help,
Mark

<Output>
<!-- NOTE: The output for the first <Set>
has been removed to make the problems
stand out better.


  Problem 1:
   Unlike the original data where there were two
   different se-tenants [105] and [107],
   here se-tenant has a single value [146];
   no label should be generated - see also
   Problem 3.
 -->
 <FormatPage souvenir-sheet="146">
   <FormatButtons>
     <Formats state="depressed" souvenir-sheet="146"/>
     <Formats label="1" state="active" se-tenant="146"/>
   </FormatButtons>
   <StampButtons>
     <Stamp label="1" state="active" pofis-number="146"/>
     <Stamp label="2" state="active" pofis-number="147"/>
     <Stamp label="3" state="active" pofis-number="148"/>
   </StampButtons>
 </FormatPage>
 <FormatPage se-tenant="146">
   <FormatButtons>
     <Formats state="active" souvenir-sheet="146"/>
     <Formats label="1" state="depressed" se-tenant="146"/>
   </FormatButtons>
   <StampButtons>
     <Stamp label="1" state="active" pofis-number="146"/>
     <Stamp label="2" state="active" pofis-number="147"/>
     <Stamp label="3" state="active" pofis-number="148"/>
   </StampButtons>
 </FormatPage>

 <!-- Problem 2:
   Although these stamps belong to the same Set,
   they do not share a format: coupon should appear
   only in the first <FormatPage> and
   souvenir-sheet only in the second.
   Neither should generate a label.
 -->
 <FormatPage coupon="244">
   <FormatButtons>
     <Formats state="depressed" coupon="244"/>
     <!-- 244 does not have a souvenir sheet -->
     <Formats label="1" state="active" souvenir-sheet="245"/>
   </FormatButtons>
   <StampButtons>
     <Stamp state="active" pofis-number="244"/>
   </StampButtons>
 </FormatPage>
 <FormatPage souvenir-sheet="245">
   <FormatButtons>
     <!-- 255 does not have a coupon -->
     <Formats state="active" coupon="244"/>
     <Formats label="1" state="depressed" souvenir-sheet="245"/>
   </FormatButtons>
   <StampButtons>
     <Stamp state="active" pofis-number="245"/>
   </StampButtons>
 </FormatPage>

 <!-- Problem 3:
   Labels for different formats should be in different sequences.
   Here se-tenant should get no label (as in the first problem),
   and the coupon labels should start with '1', not '2'. However,
were there two se-tenants, they would be labeled '1' and '2' also.
 -->
 <FormatPage souvenir-sheet="365">
   <FormatButtons>
     <Formats state="depressed" souvenir-sheet="365"/>
     <Formats label="1" state="active" se-tenant="365"/>
     <Formats label="2" state="active" coupon="367"/>
     <Formats label="3" state="active" coupon="368"/>
   </FormatButtons>
   <StampButtons>
     <Stamp label="1" state="active" pofis-number="365"/>
     <Stamp label="2" state="active" pofis-number="366"/>
     <Stamp label="3" state="active" pofis-number="367"/>
     <Stamp label="4" state="active" pofis-number="368"/>
   </StampButtons>
 </FormatPage>
 <FormatPage se-tenant="365">
   <FormatButtons>
     <Formats state="active" souvenir-sheet="365"/>
     <Formats label="1" state="depressed" se-tenant="365"/>
     <Formats label="2" state="active" coupon="367"/>
     <Formats label="3" state="active" coupon="368"/>
   </FormatButtons>
   <StampButtons>
     <Stamp label="1" state="active" pofis-number="365"/>
     <Stamp label="2" state="active" pofis-number="366"/>
   </StampButtons>
 </FormatPage>
 <FormatPage coupon="367">
   <FormatButtons>
     <Formats state="active" souvenir-sheet="365"/>
     <Formats label="1" state="active" se-tenant="365"/>
     <Formats label="2" state="depressed" coupon="367"/>
     <Formats label="3" state="active" coupon="368"/>
   </FormatButtons>
   <StampButtons>
     <Stamp state="active" pofis-number="367"/>
   </StampButtons>
 </FormatPage>
 <FormatPage coupon="368">
   <FormatButtons>
     <Formats state="active" souvenir-sheet="365"/>
     <Formats label="1" state="active" se-tenant="365"/>
     <Formats label="2" state="active" coupon="367"/>
     <Formats label="3" state="depressed" coupon="368"/>
   </FormatButtons>
   <StampButtons>
     <Stamp state="active" pofis-number="368"/>
   </StampButtons>
 </FormatPage>
</Output>

<Input>
 <!-- No Problem here, original data -->
 <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>
 <!-- Problem 1:
    Unlike the original data where there were two
    different se-tenants [105] and [107],
    here se-tenant has a single value [146];
    no label should be generated
 -->
 <Set>
   <Stamp>
     <CatNumbers pofis-number="146"/>
     <Formats souvenir-sheet="146"/>
     <Formats se-tenant="146"/>
   </Stamp>
   <Stamp>
     <CatNumbers pofis-number="147"/>
     <Formats souvenir-sheet="146"/>
     <Formats se-tenant="146"/>
   </Stamp>
   <Stamp>
     <CatNumbers pofis-number="148"/>
     <Formats souvenir-sheet="146"/>
     <Formats se-tenant="146"/>
   </Stamp>
 </Set>
 <!-- Problem 2:
   Although these stamps belong to the same Set,
   they do not share a format: coupon should appear
   only in the first <FormatPage> and
   souvenir-sheet only in the second.
 -->
 <Set>
   <Stamp>
     <CatNumbers pofis-number="244"/>
     <Formats coupon="244"/>
   </Stamp>
   <Stamp>
     <CatNumbers pofis-number="245"/>
     <Formats souvenir-sheet="245"/>
   </Stamp>
 </Set>
 <!-- Problem 3:
   Labels for different formats should be in different sequences.
   Here se-tenant should get no label (as in the first problem),
   and the coupon labels should start with '1', not '2'. However,
were there two se-tenants, they would be labeled '1' and '2' also.
 -->
 <Set>
   <Stamp>
     <CatNumbers pofis-number="365"/>
     <Formats souvenir-sheet="365"/>
     <Formats se-tenant="365"/>
   </Stamp>
   <Stamp>
     <CatNumbers pofis-number="366"/>
     <Formats souvenir-sheet="365"/>
     <Formats se-tenant="365"/>
   </Stamp>
   <Stamp>
     <CatNumbers pofis-number="367"/>
     <Formats souvenir-sheet="365"/>
     <Formats coupon="367"/>
   </Stamp>
   <Stamp>
     <CatNumbers pofis-number="368"/>
     <Formats souvenir-sheet="365"/>
     <Formats coupon="368"/>
   </Stamp>
 </Set>
</Input>
--------------------------------------

----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<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="Input">
   <xsl:element name="Output">
     <xsl:apply-templates/>
   </xsl:element>
 </xsl:template>

 <xsl:template match="Set">
   <!-- <xsl:apply-templates mode="stamp"/> Removed, works fine-->
   <xsl:apply-templates mode="formats"/>
 </xsl:template>

 <xsl:template match="Stamp" mode="formats">
   <xsl:apply-templates mode="formats"/>
 </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">
<xsl:for-each-group select="../../Stamp/Formats" group-by="concat(name(@*), '=', @*)">
<xsl:variable name="active" select="name(@*) = $format and @* = $number"/>
<xsl:element name="Formats">
<xsl:if test="position() gt 1">
<xsl:attribute name="label" select="position()-1"/>
</xsl:if>
<xsl:attribute name="state" select="if($active) then 'depressed' else 'active'"/>
<xsl:copy-of select="@*"/>
</xsl:element>
</xsl:for-each-group>
</xsl:element>
<xsl:element name="StampButtons">
<xsl:variable name="stamps" select="../../Stamp[Formats/@*[name(.)=$format][.=$number]]"/>
<xsl:for-each select="$stamps">
<xsl:element name="Stamp">
<xsl:if test="count($stamps) gt 1">
<xsl:attribute name="label" select="position()"/>
</xsl:if>
<xsl:attribute name="state" select="'active'"/>
<xsl:copy-of select="CatNumbers/@pofis-number"/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:if>
</xsl:template>


</xsl:stylesheet>

Current Thread