Re: [xsl] Grouping with keys

Subject: Re: [xsl] Grouping with keys
From: Mike Ferrando <mikeferrando@xxxxxxxxx>
Date: Thu, 9 Feb 2006 08:54:55 -0800 (PST)
Silvia L.,
I was messing around with this and came up with a solution.

I probably write things different than you do. Others on this list
would greatly simplify these templates. However, I wanted you to see
how it works. I use for-each only in special circumstances.

Hope you like it.

Mike Ferrando
Library Technician
Library of Congress
Washington, DC
202-707-4454

===XML:===
<?xml version="1.0" encoding="UTF-8"?>
<!-- <?mso-application progid="Word.Document"?> -->
<wordDocument>
  <body>
    <sect>
      <p>
        <pPr>
          <pStyle val="Heading1"/>
        </pPr>
        <r>
          <t>Erste Ueberschrift</t>
        </r>
      </p>
      <p>
        <pPr>
          <pStyle val="Programlisting"/>
        </pPr>
        <r>
          <t>&lt;HTML&gt;</t>
        </r>
      </p>
      <p>
        <pPr>
          <pStyle val="Programlisting"/>
        </pPr>
        <r>
          <t> &lt;HEAD&gt;</t>
        </r>
      </p>  
      <p>
        <pPr>
          <pStyle val="Programlisting"/>
        </pPr>
        <r>
          <t> &lt;BODY&gt;</t>
        </r>
      </p>  
    </sect>
  </body>
</wordDocument>


===XSL:===
<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
  xmlns:saxon="http://icl.com/saxon";
  extension-element-prefixes="saxon"
>
<xsl:output 
  version="1.0" 
  method="xml" 
  indent="yes" 
  encoding="iso-8859-1" 
  omit-xml-declaration="no" 
  standalone="no" 
  media-type="text/xml"
/>


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

<xsl:param name="replace"/>

<xsl:template match="/">
  <xsl:apply-templates select="//*[not(parent::*)]"/>
</xsl:template>

<xsl:template match="*[not(parent::*)]">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@* | * | text() | comment()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="p">
  <xsl:choose>      
    <xsl:when 
      test="
        pPr/pStyle[@val='Heading1']
        | 
        pPr/pStyle[@val='Programlisting']
      "
    >
      <xsl:if test="pPr/pStyle[@val='Heading1']">
        <title>
          <xsl:value-of select="r/t"/>
        </title>
      </xsl:if>
      <xsl:if test="pPr/pStyle[@val='Programlisting']">
        <xsl:if 
          test="
            count(
              preceding-sibling::p[pPr/pStyle[@val='Programlisting']]
            )=0
          "
        >
          <programlisting>
            <xsl:apply-templates 
              select="parent::*/p[pPr/pStyle[@val='Programlisting']]"
              mode="get_it"
            />
          </programlisting>
        </xsl:if>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="p[pPr/pStyle[@val='Programlisting']]"
mode="get_it">
  <xsl:text>&#xA;</xsl:text>
  <xsl:value-of select="normalize-space(r/t)"/>
  <xsl:if
test="count(following-sibling::p[pPr/pStyle[@val='Programlisting']])=0">
    <xsl:text>&#xA;</xsl:text>
  </xsl:if>
</xsl:template>

<xsl:template match="@*">
  <xsl:attribute name="{name()}">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

<!-- comment() | text() template -->
<xsl:template match="comment()">
  <xsl:copy>
    <xsl:value-of select="."/>
  </xsl:copy>
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select="translate(., '&#xA;', '')"/>
</xsl:template>

</xsl:stylesheet>


===OUTPUT:===
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<wordDocument>
   <title>Erste Ueberschrift</title>
   <programlisting>
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;BODY&gt;
</programlisting>
</wordDocument>



--- Silvia Liberto <SLiberto@xxxxxx> wrote:

> hello again,
> 
> have someone an idea for my problem?
> i don4t know where the mistake is?
> 
> i have another solution, without keys, but it doesn4t works too!?
> Maybe its a small thing!?
> 
> can someone give me a tip?
> 
> Thanks!
> 
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 
>  <xsl:template match="p">
>  <programlisting>
>   <xsl:call-template name="prog"/>
>      </programlisting> 
> </xsl:template>
> 
> <xsl:template match="r">
>   <xsl:copy-of select="."/>
> </xsl:template>
> 
> <xsl:template name="prog">
> 	  
>       <xsl:for-each  
> select="following-sibling::p[pPr/pStyle/@val='Programlisting']">
> 		  <xsl:value-of select="." />  
> 	  </xsl:for-each>  
>     		 	  
> 
> </xsl:template>
> 
> 
> </xsl:stylesheet>
> 
> 
> 
> 
> > Hi Wendell and all,
> > 
> > thanks for helping, i know what you mean when you say i should
> try a
> > simpler
> > solution, but i need this key! ;-)
> > 
> > so i have changed my syntax on the @use attribute for
> Programlisting.
> > 
> > >
> generate-id(preceding-sibling::p[pPr/pStyle/@val='Heading1'][1])
> > 
> > but it still doesn4t works!? i have no grouping effect?
> > 
> > i can4t find the reason, have you another idea?
> 
> the output shoud be like this:
> 
> <programlisting>
> &lt;HTML&gt;
> &lt;HEAD&gt;
> &lt;BODY&gt;
> </programlisting>
> 
> > 
> > 
> > <xsl:key name="prog" match="p[pPr/pStyle/@val='Programlisting']"
> > 
> >
>
use="generate-id(preceding-sibling::p[pPr/pStyle/@val='Programlisting'][1])"/>
> >  
> > 
> >  <xsl:template match="p">
> >  <xsl:choose>			
> > 		<xsl:when test="pPr/pStyle/@val='Heading1'">
> > 			<title>
> > 				<xsl:value-of select="r/t"/>
> > 			</title>
> > 		</xsl:when>
> > 		<xsl:when test="pPr/pStyle/@val='Programlisting'">
> > 	<programlisting>
> > 			 <xsl:for-each select="key('prog',generate-id())">
> > 				<xsl:value-of select="r/t"/>
> > 			 </xsl:for-each>	 
> > 		</programlisting>
> > 		</xsl:when>
> > </xsl:choose>
> > 
> > 
> > </xsl:template>
> > 
> > </xsl:stylesheet>
> > 
> > 
> > 
> > XML:
> > 
> > <?xml version="1.0" encoding="UTF-8"?>
> > <!--<?mso-application progid="Word.Document"?>-->
> > <wordDocument>
> > 	<body>
> > 		<sect>
> > 			<p>
> > 				<pPr>
> > 					<pStyle val="Heading1"/>
> > 				</pPr>
> > 				<r>
> > 					<t>Erste Ueberschrift</t>
> > 				</r>
> > 			</p>
> > 			<p>
> > 				<pPr>
> > 					<pStyle val="Programlisting"/>
> > 				</pPr>
> > 				<r>
> > 					<t>&lt;HTML&gt;</t>
> > 				</r>
> > 			</p>
> > 			<p>
> > 				<pPr>
> > 					<pStyle val="Programlisting"/>
> > 				</pPr>
> > 				<r>
> > 					<t> &lt;HEAD&gt;</t>
> > 				</r>
> > 			</p>	
> > 			<p>
> > 				<pPr>
> > 					<pStyle val="Programlisting"/>
> > 				</pPr>
> > 				<r>
> > 					<t> &lt;BODY&gt;</t>
> > 				</r>
> > 			</p>	
> > 		</sect>
> > 	</body>
> > </wordDocument>
> > 
> > 
> > 
> > 
> > -- 
> > Telefonieren Sie schon oder sparen Sie noch?
> > NEU: GMX Phone_Flat http://www.gmx.net/de/go/telefonie
> > 
> 
> -- 
> DSL-Aktion wegen gro_er Nachfrage bis 28.2.2006 verldngert:
> GMX DSL-Flatrate 1 Jahr kostenlos* http://www.gmx.net/de/go/dsl
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Current Thread