Preserving attribute whitespace

前端 未结 4 1266
说谎
说谎 2020-12-01 18:54

Disclaimer: the following is a sin against XML. That\'s why I\'m trying to change it with XSLT :)

My XML currently looks like this:


            


        
相关标签:
4条回答
  • 2020-12-01 19:37

    This is actually a raw XML parsing problem, not something XSLT can help you with. An XML parse must convert the newlines in that attribute value to spaces, as per ‘3.3.3 Attribute-Value Normalization’ in the XML standard. So anything currently reading your description attributes and keeping the newlines in is doing it wrong.

    You may be able to recover the newlines by pre-processing the XML to escape the newlines to & #10; character references, as long as you haven't also got newlines where charrefs are disallowed, such as inside tag bodies. Charrefs should survive as control characters through to the attribute value, where you can then turn them into text nodes.

    0 讨论(0)
  • 2020-12-01 19:39

    As others have pointed out, the XML spec doesn't allow for the preservation of spaces in attributes. In fact, this is one of the few differentiators between what you can do with attributes and elements (the other main one being that elements can contain other tags while attributes cannot).

    You will have to process the file outside of XML first in order to preserve the spaces.

    0 讨论(0)
  • 2020-12-01 19:45

    If you can control your XML processor, you can do it.

    From my other answer (which has many references linked):

    if you have an XML like

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE elemke [
    <!ATTLIST brush wood CDATA #REQUIRED>
    ]>
    
    <elemke>
    <brush wood="guy&#xA;threep"/>
    </elemke>
    

    and an XSL like

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template name="split">
      <xsl:param name="list"      select="''" />
      <xsl:param name="separator" select="'&#xA;'" />
      <xsl:if test="not($list = '' or $separator = '')">
        <xsl:variable name="head" select="substring-before(concat($list, $separator), $separator)" />
        <xsl:variable name="tail" select="substring-after($list, $separator)" />
    
        <xsl:value-of select="$head"/>
        <br/><xsl:text>&#xA;</xsl:text>
        <xsl:call-template name="split">
            <xsl:with-param name="list"      select="$tail" />
            <xsl:with-param name="separator" select="$separator" />
        </xsl:call-template>
      </xsl:if>
    </xsl:template>
    
    
    <xsl:template match="brush">
      <html>
      <xsl:call-template name="split">
        <xsl:with-param name="list" select="@wood"/>
      </xsl:call-template>
      </html>
    </xsl:template>
    
    </xsl:stylesheet>
    

    you can get a html like:

    <html>guy<br>
       threep<br>
    
    </html>  
    

    as tested/produced with a processor like this saxon command line:

    java -jar saxon9he.jar -s:in.xml -xsl:in.xsl -o:out.html
    
    0 讨论(0)
  • 2020-12-01 19:49

    According to the Annotated XML Spec, white space in attribute values are normalized by the XML processor (See the (T) annotation on 3.3.3). So, it looks like the answer is probably no.

    0 讨论(0)
提交回复
热议问题