How to concatenate the contents of all children of a node in XPath?

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2010-06-03T19:51:52Z Indexed on 2010/06/03 20:54 UTC
Read the original article Hit count: 213

Filed under:
|

Is it possible with XPath to get a concatenated view of all of the children of a node? I am looking for something like the JQuery .html() method.

For example, if I have the following XML:

<h3 class="title">
    <span class="content">this</span>
    <span class="content"> is</span>
    <span class="content"> some</span>
    <span class="content"> text</span>
</h3>

I would like an XPath query on "h3[@class='title']" that would give me "this is some text".

That is the real question, but if more context/background is helpful, here it is: I am using XPath and I used this post to help me write some complex XSL. My source XML looks like this.

<h3 class="title">Title</h3>
<p>
    <span class="content">Some</span>
    <span class="content"> text</span>
    <span class="content"> for</span>
    <span class="content"> this</span>
    <span class="content"> section</span>
</p>
<p>
    <span class="content">Another</span>
    <span class="content"> paragraph</span>
</p>
<h3 class="title">
    <span class="content">Title</span>
    <span class="content"> 2</span>
    <span class="content"> is</span>
    <span class="content"> complex</span>
</h3>
<p>
    <span class="content">Here</span>
    <span class="content"> is</span>
    <span class="content"> some</span>
    <span class="content"> text</span>
</p>

My output XML considers each <h3> as well as all <p> tags until the next <h3>. I wrote the XSL as follows:

<xsl:template match="h3[@class='title']">
...
    <xsl:apply-templates select="following-sibling::p[
        generate-id(preceding-sibling::h3[1][@class='title'][text()=current()/text()])
        =
        generate-id(current())
    ]"/>
...
</xsl:template>

The problem is that I use the text() method to identify h3s that are the same. In the example above, the "Title 2 is complex" title's text() method returns whitespace. My thought was to use a method like JQuery's .html that would return me "Title 2 is complex"

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xslt