Implement xs:collapse for @xml:space in XSLT 1.0

lx80.com

New Member
The value of the attribute \[code\]@xml:space\[/code\] can be either \[code\]"default"\[/code\] or \[code\]"preserve"\[/code\]. XML specifies what the second means but leaves the first up to the application. (I think I have that correct.) So what if the application wants \[code\]default\[/code\] to implement \[code\]xs:collapse\[/code\]? How could XSLT 1.0 actually do this?I think the built-in template for processing text, that is,\[code\]<xsl:template match="text()"> <xsl:value-of select="."/></xsl:template>\[/code\]would need to be replaced with something like this pseudo-code:\[code\]<xsl:choose> <xsl:when test="../@xml:space='preserve'" <xsl:value-of select="."/> </xsl:when> <xsl:otherwise> if position(.)=1 then output LTRIM(value-of(.)) if position(.)=last() then output RTRIM(value-of(.)) if position(.)= 1 and last()=1 then output normalize-space(.) </xsl:otherwise></xsl:choose>\[/code\]This input then:\[code\]<persName> The man is <forename>Edward</forename> <forename>George</forename> <surname type="linked">Bulwer-Lytton</surname>, <roleName>Baron Lytton of <placeName>Knebworth</placeName> </roleName></persName>\[/code\]would get rendered correctly as \[code\]The man is Edward George Bulwer-Lytton, Baron Lytton of Knebworth\[/code\] with the space before \[code\]The man\[/code\] and after \[code\]Knebworth\[/code\] trimmed and the spaces between \[code\]Edward\[/code\] and \[code\]George\[/code\] collapsed. (The example is from TEI.)[EDIT: I removed an incorrect and misleading paragraph here.]The XSLT 1.0 to implement that pseudo-code would need to be executed for every text node. Wouldn't that be ugly and slow? [EDIT: Or maybe not. I simplified the pseudo code. Are there fast trim routines? Is the choose really that slow?]Bottom line: How does one implement xs:collapse in XSLT 1.0 (with only browser-embedded extensions)?I hope I'm saying all that correctly. And I hope the code is simple. I haven't yet seen how it can be.
 
Back
Top