[ Transform inline XML elements with XSLT ]
Does anyone know what XSLT to use to transform the following inline XML elements into the corresponding HTML?
XML:
<line><b c="foo1" /> bar <b c="foo2" /> bar <b c="foo3" /> bar</line>
HTML:
<p><span class="x">foo1</span> bar <span class="x">foo2</span> bar <span class="x">foo3</span> bar </p>
- 'line' becomes 'p'
- each 'b' becomes a 'span' with a class 'x' that I will provide
- the contents of each span will be the 'c' attribute of the corresponding 'b'
I can iterate over each 'line' in my file, and I can iterate over each 'b' in each line, however in the output the entire text content of 'line' is output then the attributes are appended after the text. Here is the code I am using. I understand why the following code does not do what I want. I just don't know how to write the XSLT to do what I want.
<xsl:for-each select=".../line">
<p>
<xsl:value-of select="text"/>
<xsl:for-each select="text/b">
<span class="x">
<xsl:value-of select="@c"/>
</span>
</xsl:for-each>
</p>
</xsl:for-each>
Answer 1
<xsl:template match="line">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="b[@c]">
<span class="x">
<xsl:value-of select="@c"/>
</span>
</xsl:template>
should suffice (as text nodes are copied by the built-in templates).