21

I am attempting to write XSLT that will run a for-each on the selected following-siblings but stop when another tag (h1) is reached.

Here's the Source XML:

<?xml version="1.0"?>
<html>
    <h1>Test</h1>
    <p>Test: p 1</p>
    <p>Test: p 2</p>
    <h1>Test 2</h1>
    <p>Test2: p 1</p>
    <p>Test2: p 2</p>
    <p>Test2: p 3</p>
</html>

Here's the XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <content>
            <xsl:apply-templates/>
        </content>
    </xsl:template>

    <xsl:template match="h1">
        <section>
            <sectionHeading>
                <xsl:apply-templates/>
            </sectionHeading>
            <sectionContent>
                <xsl:for-each select="following-sibling::p">
                    <paragraph>
                        <xsl:value-of select="."/>
                    </paragraph>
                </xsl:for-each>
            </sectionContent>
        </section>
    </xsl:template>

    <xsl:template match="p"/>
</xsl:stylesheet>

Here's the current result:

<?xml version="1.0" encoding="UTF-8"?>
<content>
    <section>
        <sectionHeading>Test</sectionHeading>
        <sectionContent>
            <paragraph>Test: p 1</paragraph>
            <paragraph>Test: p 2</paragraph>
            <paragraph>Test: p 3</paragraph>
            <paragraph>Test2: p 1</paragraph>
            <paragraph>Test2: p 2</paragraph>
        </sectionContent>
    </section>
    <section>
        <sectionHeading>Test 2</sectionHeading>
        <sectionContent>
            <paragraph>Test2: p 1</paragraph>
            <paragraph>Test2: p 2</paragraph>
        </sectionContent>
    </section>
</content>

Here's the expected result:

<?xml version="1.0" encoding="UTF-8"?>
<content>
<section>
    <sectionHeading>Test</sectionHeading>
    <sectionContent>
        <paragraph>Test: p 1</paragraph>
        <paragraph>Test: p 2</paragraph>
        <paragraph>Test: p 3</paragraph>
    </sectionContent>
</section>
<section>
    <sectionHeading>Test 2</sectionHeading>
    <sectionContent>
        <paragraph>Test2: p 1</paragraph>
        <paragraph>Test2: p 2</paragraph>
    </sectionContent>
</section>
</content>
1
  • @ Tim,though we can get a solution for xslt i would rather suggest you to change the xml format to <Test> <h1>Test</h1> <p>Test: p 1</p> <p>Test: p 2</p> </Test> <Test> <h1>Test 2</h1> <p>Test2: p 1</p> <p>Test2: p 2</p> <p>Test2: p 3</p> </Test> Which makes more sense and is readable,also easy to query in xslt.
    – Ravisha
    Jan 30, 2010 at 4:05

2 Answers 2

27

Try this: (Instead of asking for all the p's we ask for all the p's whose most recently preceding h1 is current.)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <content>
            <xsl:apply-templates/>
        </content>
    </xsl:template>

    <xsl:template match="h1">
        <xsl:variable name="header-id" select="generate-id(.)"/>
        <section>
            <sectionHeading>
                <xsl:apply-templates/>
            </sectionHeading>
            <sectionContent>
                <xsl:for-each select="following-sibling::p[generate-id(preceding-sibling::h1[1]) = $header-id]">
                    <paragraph>
                        <xsl:value-of select="."/>
                    </paragraph>
                </xsl:for-each>
            </sectionContent>
        </section>
    </xsl:template>

    <xsl:template match="p"/>
</xsl:stylesheet>
1
  • Just tested this out and it's working perfectly. Thank you very much.
    – Tim
    Jan 29, 2010 at 22:39
11

The accepted answer has a bad side effect and it is kinda wrong.

Further in this post, i will explain the real compare of the following essential statement and why it can and will fail.


recap/analyse situation while being in template <xsl:template match="h1">:

  • Current context node is any h1 from the matching <xsl:template>.
  • variable named header contains a duplicate of my current context node.

The essential statement which is bad/wrong:

following-sibling::p[preceding-sibling::h1[1] = $header]

  • select all following siblings p of my context node | following-sibling::p
  • filter these p where the first (closest) preceding-sibling named h1 "is" the same as the variable $header | ...[preceding-sibling::h1[1] = $header].

!! In XSLT 1.0 the compare of a node with a node will be done by its value !!


See it in an example. Lets pretend the input xml is like this [<h1> contain twice the same value Test]:

<html>
    <h1>Test</h1>
    <p>Test: p 1</p>
    <p>Test: p 2</p>
    <h1>Test</h1>
    <p>Test2: p 1</p>
    <p>Test2: p 2</p>
    <p>Test2: p 3</p>
</html>

A !WRONG! result will be created:

<content>
  <section>
     <sectionHeading>Test</sectionHeading>
     <sectionContent>
        <paragraph>Test: p 1</paragraph>
        <paragraph>Test: p 2</paragraph>
        <paragraph>Test2: p 1</paragraph> <-- should be only in 2. section 
        <paragraph>Test2: p 2</paragraph> <-- should be only in 2. section 
        <paragraph>Test2: p 3</paragraph> <-- should be only in 2. section 
     </sectionContent>
  </section>
  <section>
     <sectionHeading>Test</sectionHeading>
     <sectionContent>
        <paragraph>Test2: p 1</paragraph>
        <paragraph>Test2: p 2</paragraph>
        <paragraph>Test2: p 3</paragraph>
     </sectionContent>
  </section>
</content>

Correct compare

...
<xsl:template match="h1">
    <xsl:variable name="header" select="generate-id(.)"/>
    <section>
        <sectionHeading>
            <xsl:apply-templates/>
        </sectionHeading>
        <sectionContent>
            <xsl:for-each select="following-sibling::p[generate-id(preceding-sibling::h1[1]) = $header]">
                <paragraph>
                    <xsl:value-of select="."/>
                </paragraph>
            </xsl:for-each>
        </sectionContent>
    </section>
</xsl:template>
...

Use the function generate-id() to get the unique (at least in the current document) ID of a node and compare now node vs node! Even if you use this technique with <xsl:key>, you have to use generate-id().

2
  • 2
    Why not just edit the accepted solution and add a comment?
    – Kyle Butt
    Feb 21, 2018 at 19:07
  • I downvoted this because you could very well have edited the correct response or left a comment.
    – Kyle Butt
    Dec 1, 2020 at 0:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.