This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]