Cannot get xslt to output an (&) even after escaping the character

后端 未结 13 1730
轮回少年
轮回少年 2020-12-17 10:13

I am trying to create a query string of variable assignments separated by the & symbol (ex: \"var1=x&var2=y&...\"). I plan to pass this

相关标签:
13条回答
  • 2020-12-17 10:32

    If you are trying to produce an XML file as output, you will want to produce & (as & on it's own is invalid XML). If you are just producing a string then you should set the output mode of the stylesheet to text by including the following as a child of the xsl:stylesheet

    <xsl:output method="text"/>
    

    This will prevent the stylesheet from escaping things and <xsl:value-of select="'&amp;'" /> should produce &.

    0 讨论(0)
  • 2020-12-17 10:34

    You can combine disable-output-escaping with a CDATA section. Try this:

    <xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>
    
    0 讨论(0)
  • 2020-12-17 10:35

    Just replace & with

    <![CDATA[&]]>
    

    in the data. EX: Data XML

    <title>Empire <![CDATA[&]]> Burlesque</title>
    

    XSLT tag:

    <xsl:value-of select="title" />
    

    Output: Empire & Burlesque

    0 讨论(0)
  • 2020-12-17 10:37

    Use disable-output-escaping="yes" in your value-of tag

    0 讨论(0)
  • 2020-12-17 10:38

    You should note, that you can use disable-output-escaping within the value of node or string/text like:

    <xsl:value-of select="/node/here" disable-output-escaping="yes" />
    

    or

    <xsl:value-of select="'&amp;'" disable-output-escaping="yes" />
    <xsl:text disable-output-escaping="yes">Texas A&amp;M</xsl:text>
    

    Note the single quotes in the xsl:value-of.

    However you cannot use disable-output-escaping on attributes. I know it's completely messed up but, that's the way things are in XSLT 1.0. So the following will NOT work:

    <xsl:value-of select="/node/here/@ttribute" disable-output-escaping="yes" />
    

    Because in the fine print is the quote:

    Thus, it is an error to disable output escaping for an <xsl:value-of /> or <xsl:text /> element that is used to generate the string-value of a comment, processing instruction or attribute node;

    emphasis mine.

    Taken from: http://www.w3.org/TR/xslt#disable-output-escaping

    0 讨论(0)
  • 2020-12-17 10:39

    Using the disable-output-escaping attribute (a boolean) is probably the easiest way of accomplishing this. Notice that you can use this not only on <xsl:value-of/> but also with <xsl:text>, which might be cleaner, depending on your specific case.

    Here's the relevant part of the specification: http://www.w3.org/TR/xslt#disable-output-escaping

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