I am transforming some generated DocBook xml (from Doxygen) to my companies xml which is really a subset of DocBook. There is a para element like the following:
I had to correct a bit of your XML example so that it was well-formed. But the following:
<xsl:template match="para">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="self::text() and normalize-space(.)!=''">
<xsl:element name="para">
<xsl:apply-templates select="."/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="literallayout">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="table">
<xsl:copy-of select="."/>
</xsl:template>
Outputs:
<para>some text..... </para>
<literallayout>
</literallayout>
<para> more text.... </para>
<table> ... </table>
<para> even more text </para>
<table>...</table>
<literallayout>text also look here <link xlink:href="http://someurl.com"/></literallayout>
<para> more text. </para>
I hope that helps.
I should use these templates:
<xsl:template match="para">
<xsl:apply-templates select="node()" mode="flat" />
</xsl:template>
<xsl:template match="*" mode="flat">
<xsl:copy-of select="." />
</xsl:template>
<xsl:template match="text()[normalize-space()!='']" mode="flat">
<para>
<xsl:value-of select="."/>
</para>
</xsl:template>
<xsl:template match="text()[normalize-space()='']" mode="flat" />
You can turn every text node within a para
element into its own para
using something like
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="para">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="para/text()">
<para><xsl:value-of select="." /></para>
</xsl:template>
</xsl:stylesheet>
but this may not be sufficient if you only want to break up the para at certain child elements and not others.