xsl-list
[Top] [All Lists]

[xsl] Outputting White Space Only Nodes from Variables

2021-02-21 20:13:51
I'm receiving user input through an application and part of what the user can 
input are generated text strings that can be placed above, before, after, and 
below element content. So here's a simple example of the input XML:
<test>   <p class="title">Book Title</p>   <p class="heading">Chapter 
1</p></test>
And here's how a transform to process the content will look with the 
information about the generated text to use captured in variables:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ;   
xmlns:xs="http://www.w3.org/2001/XMLSchema" ;   exclude-result-prefixes="xs"    
version="2.0">
    <xsl:output method="xml" indent="yes"/>        <xsl:template match="test">  
      <output>            <xsl:apply-templates/>        </output>    
</xsl:template>
    <xsl:template match="*">        <xsl:copy>            <xsl:copy-of 
select="@*"/>            <xsl:apply-templates/>        </xsl:copy>    
</xsl:template>
    <xsl:template match="p[@class='title']">        <xsl:variable 
name="generatedTextBefore" as="xs:string">A </xsl:variable>        
<xsl:variable name="generatedTextAfter" as="xs:string?"> </xsl:variable>        
<xsl:variable name="generatedTextAbove" as="xs:string">Short Stories 
Collection</xsl:variable>        <xsl:variable name="generatedTextBelow" 
as="xs:string?">&#x0A;</xsl:variable>        <xsl:variable name="class" 
as="xs:string">            <xsl:choose>                <xsl:when 
test="@class">                    <xsl:value-of select="@class"/>               
 </xsl:when>                <xsl:otherwise>none</xsl:otherwise>            
</xsl:choose>        </xsl:variable>        <xsl:if 
test="not($generatedTextAbove = '')">            <xsl:message>ABOVE: Gen text 
value: |<xsl:copy-of select="$generatedTextAbove"/>|</xsl:message>            
<xsl:element name="pre">                <xsl:attribute name="class" 
select="concat($class, '_above')"/>                <xsl:value-of 
select="$generatedTextAbove"/>            </xsl:element>        </xsl:if>       
 <xsl:copy>            <xsl:copy-of select="@*"/>            <xsl:if 
test="not($generatedTextBefore = '')">                <xsl:message>BEFORE Gen 
text value: |<xsl:copy-of select="$generatedTextBefore"/>|</xsl:message>        
        <xsl:element name="gt">                    <xsl:attribute name="class" 
select="concat($class, '_before')"/>                    <xsl:value-of 
select="$generatedTextBefore"/>                </xsl:element>            
</xsl:if>            <!-- output the element content here -->
            <xsl:apply-templates />            <xsl:if 
test="not($generatedTextAfter = '')">                <xsl:message>AFTER Gen 
text value: |<xsl:copy-of select="$generatedTextAfter"/>|</xsl:message>         
       <xsl:element name="gt">                    <xsl:attribute name="class" 
select="concat($class, '_after')"/>                    <xsl:value-of 
select="$generatedTextAfter"/>                </xsl:element>            
</xsl:if>        </xsl:copy>        <xsl:if test="not($generatedTextBelow = 
'')">            <xsl:message>BELOW Gen text value: |<xsl:copy-of 
select="$generatedTextBelow"/>|</xsl:message>            <xsl:element 
name="pre">                <xsl:attribute name="class" select="concat($class, 
'_below')"/>                <xsl:value-of select="$generatedTextBelow"/>        
    </xsl:element>        </xsl:if>    </xsl:template>    </xsl:stylesheet>
Given the above, what I'm wanting in the result document is this:
<output>   <pre class="title_above">Short Stories Collection</pre>   <p 
class="title">      <gt class="title_before">A </gt>Book Title<gt 
class="title_after"> </gt>   </p>   <pre class="title_below"></pre>   <p 
class="heading">Chapter 1</p></output>
But the variables with white space only content evidently get that content 
stripped out. I've tried all of the following:
1. Use different instructions for the output of the variable: value-of, 
copy-of, and sequence2. Created the variables as both strings and elements and 
even RTFs3. Tried a character map4. Tried an alternative approach like using 
strings "#SPACE, #TAB, #NL, etc. I know that would work but seems like it 
should be unnecessary?
In every case the white space only variable content gets stripped out. How can 
such content be output?
Thanks,
Don
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>