Rob Mason
Rob Mason

Reputation: 1417

Transforming XML with XSL and self closing tags

I have the following XML

<run>
    <font style="bold"/>Some Wording
</run>

I want to transform it to

<p>
    <b>Some Wording</b>
</p>

I am trying this template

<xsl:template match="run/font[@style='bold']">
    <p>
        <b>
            <xsl:value-of select="."/>
        </b>
    </p>
</xsl:template>

but this gives the output

<p>
    <b/>Some Wording
</p>

How do I wrap the wording in the <b> tags using XSLT?

Upvotes: 1

Views: 179

Answers (1)

Michael Kay
Michael Kay

Reputation: 163312

If your requirement is to wrap any text node that is immediately preceded by <font style="bold"/> in a <b> element, you can achieve that with

<xsl:template match="text()[preceding-sibling::*[1][self::font][@style='bold']]">
  <b><xsl:value-of select="."/></b>
</xsl:template>

If that's not your requirement (inferring requirements from one example is notoriously error-prone) then you need to explain it more clearly.

Upvotes: 2

Related Questions