Re: [xsl] Grouping by attribute

Subject: Re: [xsl] Grouping by attribute
From: Jostein Austvik Jacobsen <josteinaj@xxxxxxxxx>
Date: Tue, 20 Oct 2009 13:17:42 +0200
Yes XSLT seems very nice, however it's not very human-readable. Thanks
for that last of your two templates - it spares me the trouble of
defining namespaces for each and every defined tag :).

As a side-question; Is it possible to rewrite any two XSLT documents
running in sequence into a single XSLT document?

-Jostein

2009/10/20 Michael Kay <mike@xxxxxxxxxxxx>:
>>
>> Now, what if I want to replace all <p>-tags with <para>, and
>> change the namespace of all nodes to "foo"?
>
> The nice thing about XSLT is that it keeps different rules separate. I
> showed you a rule for processing the contentSection, now you want a rule
for
> processing <p> elements, and another rule all other elements. So you want
> three template rules for the three cases.
>
> <xsl:template match="p">
>  <para><xsl:apply-templates/></para>
> </xsl:template>
>
> <xsl:template match="*">
>  <xsl:element name="{local-name()}" namespace="foo">
>    <xsl:copy-of select="@*"/>
>    <xsl:apply-templates/>
>  </xsl:element>
> </xsl:template>
>
>>
>> I've tried adding xmlns="foo" to the stylesheet tag.
>
> That will cause any elements created by the stylesheet as literal result
> elements to be in namespace foo. But it won't change the namespace of
> elements copied from the source document.
>
> Regards,
>
> Michael Kay
> http://www.saxonica.com/
> http://twitter.com/michaelhkay
>
>
>>
>> Regards
>> Jostein
>> Newbie
>>
>> 2009/10/20 Jostein Austvik Jacobsen <josteinaj@xxxxxxxxx>:
>> > Thanks! I've been testing it a bit now and it works as expected.
>> >
>> > I'm having a little trouble mixing it with some of my other
>> templates
>> > used on other parts of the document but I'll probably figure it out.
>> > Or else, I'll ask.
>> >
>> > Regards
>> > Jostein
>> >
>> > 2009/10/19 Michael Kay <mike@xxxxxxxxxxxx>:
>> >> The essence of the solution is
>> >>
>> >> <xsl:template match="contentSection">
>> >>  <h1>Title</h1>
>> >>  <xsl:for-each-group select="*" group-starting-with="headline">
>> >>    <xsl:choose>
>> >>      <xsl:when test="headline">
>> >>        <level2>
>> >>          <xsl:apply-templates select="current-group()"/>
>> >>        </level2>
>> >>      </xsl:when>
>> >>      <xsl:otherwise>
>> >>        <xsl:apply-templates select="current-group()"/>
>> >>      </xsl:otherwise>
>> >>    </
>> >>  </
>> >> </
>> >>
>> >> Regards,
>> >>
>> >> Michael Kay
>> >> http://www.saxonica.com/
>> >> http://twitter.com/michaelhkay
>> >>
>> >>> -----Original Message-----
>> >>> From: Jostein Austvik Jacobsen [mailto:josteinaj@xxxxxxxxx]
>> >>> Sent: 19 October 2009 13:18
>> >>> To: xsl-list
>> >>> Subject: [xsl] Grouping by attribute
>> >>>
>> >>> I've got this flat-structured XML:
>> >>>
>> >>> <document>
>> >>>   <metaData>
>> >>>     <title>Title</title>
>> >>>     <publisher>Publisher</publisher>
>> >>>   </metaData>
>> >>>   <contentSection>
>> >>>     <p>text</p>
>> >>>
>> >>>     <headline level="2">Headline</headline>
>> >>>     <p>text</p>
>> >>>     <p>text</p>
>> >>>
>> >>>     <headline level="2">Section</headline>
>> >>>     <p>text</p>
>> >>>     <pagenum id="page2"/>
>> >>>     <p>text</p>
>> >>>     <headline level="3">Subsection</headline>
>> >>>     <p>text</p>
>> >>>
>> >>>     <headline level="2">Section</headline>
>> >>>     <p>text</p>
>> >>>     <p>text</p>
>> >>>   </contentSection>
>> >>> </document>
>> >>>
>> >>> And want it transformed to this XML:
>> >>>
>> >>> <body>
>> >>>   <level1>
>> >>>     <h1>Title</h1>
>> >>>     <p>text</p>
>> >>>
>> >>>     <level2>
>> >>>       <h2>Headline</h2>
>> >>>       <p>text</p>
>> >>>       <p>text</p>
>> >>>     </level2>
>> >>>
>> >>>     <level2>
>> >>>       <h2>Section</h2>
>> >>>       <p>text</p>
>> >>>       <pagenum id="page2"/>
>> >>>       <p>text</p>
>> >>>       <level3>
>> >>>         <h3>Subsection</h3>
>> >>>         <p>text</p>
>> >>>       </level3>
>> >>>     </level2>
>> >>>
>> >>>     <level2>
>> >>>       <h2>Section</h2>
>> >>>       <p>text</p>
>> >>>       <p>text</p>
>> >>>     </level2>
>> >>>   </level1>
>> >>> </body>
>> >>>
>> >>> How would I do that using XSLT? XSLT 2.0 solutions are ok.
>> >>>
>> >>> Regards
>> >>> Jostein

Current Thread