Reputation: 1934
I have an XML document which contains both structure (section) tags and display tags (columns):
<document>
<new-column/>
<section1>
text
</section1>
<section2>
text
<new-column/>
text
</section2>
</document>
I am writing an XSLT to display this as HTML, and the logical way would be to use a <div>
tag to display the text in separate columns, so I'd have to convert <new-column>
into <div>
.
However, the <div>
requires a closing </div>
tag, with all the comprising elements in between, but then the document would not be well-formed, with <section>
and <new-column>
being wrongly nested.
One solution would be to add closing </section>
tags like this:
<document>
<new-column>
<section1>
text
</section1>
<section2>
text
</section2>
</new-column>
<new-column>
<section2>
text
</section2>
</new-column>
</document>
However, I was wondering if anyone has a better idea, such as adding text to a div section on the fly, or perhaps using a different solution for multiple column (and multiple pages) display. TIA
Upvotes: 0
Views: 109
Reputation: 116959
Would something like this work for you?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="text-by-col" match="text()" use="generate-id(preceding::new-column[1])" />
<xsl:template match="/">
<document>
<xsl:for-each select=".//new-column">
<div>
<xsl:apply-templates select="key('text-by-col', generate-id())" mode="span"/>
</div>
</xsl:for-each>
</document>
</xsl:template>
<xsl:template match="text()" mode="span">
<span id="{local-name(parent::*)}"><xsl:copy/></span>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Upvotes: 1