|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Siblings to hierarchy: Better way to do this?
Dear All,
I need to transform stuff in this form: <r>
<section/>
<paragraph>adolf</paragraph>
<paragraph>alex</paragraph>
<section/>
<paragraph>bertram</paragraph>
<paragraph>bilbo</paragraph>
<paragraph>bumble</paragraph>
<section/>
<paragraph>claus</paragraph>
<paragraph>charles</paragraph>
<paragraph>cymbaline</paragraph>
</r>to this: <r>
<section>
<Para>adolf</Para>
<Para>alex</Para>
</section>
<section>
<Para>bertram</Para>
<Para>bilbo</Para>
<Para>bumble</Para>
</section>
<section>
<Para>claus</Para>
<Para>charles</Para>
<Para>cymbaline</Para>
</section>
</r>The style-sheet which worked seems clumsy to me: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <r> <xsl:apply-templates select="/r/section"/> </r> </xsl:template> <xsl:template match="section"> <xsl:variable name="gid" select="generate-id()"/> <section> <xsl:apply-templates select="following-sibling::paragraph[generate-id(preceding-sibling::section[1])=$gid]"/> </section> </xsl:template> <xsl:template match="paragraph"> <Para> <xsl:apply-templates/> </Para> </xsl:template> </xsl:stylesheet> Can anyone suggest something faster and/or more elegant please? Also: I don't understand why this didn't work: if anyone can explain, I'd be grateful: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <r> <xsl:apply-templates select="/r/section"/> </r> </xsl:template> <xsl:template match="section"> <xsl:variable name="sect" select="self::node()"/> <section> <!-- match only paragraphs whose first preceding sibling is the section we are processing --> <xsl:apply-templates select="following-sibling::paragraph[preceding-sibling::section[1]=$sect]"/> </section> </xsl:template> <xsl:template match="paragraph"> <Para> <xsl:apply-templates/> </Para> </xsl:template> </xsl:stylesheet> Thank you, Richard Bondi XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








