[Home] [By Thread] [By Date] [Recent Entries]
Stephan Kahnt wrote:
Am Freitag, 13. Oktober 2006 12:49 schrieb Andrew Welch: Hi Stephan, Did it work out with the comments? I was thinking today alongside some other option that you might want to try, if you want to do this using purely XSLT 2.0. There's an instruction, <xsl:character-maps> which you can use to achieve your goal. The other day I wanted output as XML, but needed to intersperse it with <br> (no closing tag) and found a simple resolution for it in XSLT. This, no question, violates the soul of XML, and a while later I convinced people at the other side of the table to go with <br /> (it appeared a minor fix). Nevertheless, here's what I found if you want to go that way with your empty tags: I assume there's a place in the code where you can test for the tag ending up empty, or you can apply micro-pipelining (term by Wendell Piez?). Here's a sample that is standalone, to illustrate it what I mean with using character maps: <xsl:stylesheet version="2.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" > <xsl:output method="xml" indent="yes" use-character-maps="empty-a"/> <xsl:character-map name="empty-a"> <!-- declare a char from priv. use area, to map to 'long' tag version of a-tag --> <xsl:output-character character="" string="<a></a>"/> </xsl:character-map> <xsl:template match="tag[normalize-space()]" mode="a" >
<a>
<xsl:value-of select="." />
</a>
</xsl:template><xsl:template match="tag[not(normalize-space())]" mode="a" > <xsl:text></xsl:text> </xsl:template> <xsl:template match="/" name="main" > <xsl:variable name="test-a"> <tag>with content</tag> <tag></tag> </xsl:variable> <xsl:apply-templates select="$test-a/tag" mode="a"/> </xsl:template> </xsl:stylesheet> This will output the following when used with Saxon 8: <a>with content</a><a></a> I recommend you to use characters from the private use area U+E000 - U+F8FF (you may also decide to use Plane-1 and Plane-16, I believe). This is to be sure not to mix up with normal unicode characters. In addition, you may decide to add readability by applying some <!ENTITY xxx yyy> references to your header, using easier mnemonics with character entities. You will have to do this for every element that may become empty. So, a micro-pipeline may really be the best option here. Be careful, you can really mess up XML with this, making it totally illegal. HtH, Cheers, -- Abel Braaksma http://www.nuntia.com
|

Cart



