How to show & in a XML attribute That would be pro

2019-09-19 13:30发布

First I want to specify my goal. I am tasked to show a url concatenated to a constant string particularly '&src=JB-10081' as an attribute of xml tag in the output.

Here is my code:

    <vacancy>
        <xsl:attribute name = "url">
          <xsl:value-of disable-output-escaping="yes" select="concat(apply_url, '&amp;src=JB-10081')"/>
        </xsl:attribute>
    <vacancy>

This code gives me a result of:

 <vacancy id="6747232000011108" datestart="" dateend="" language="" origin="" url="https://dtt.taleo.net/careersection/10540/jobapply.ftl?lang=en-GB&amp;job=27050&amp;src=JB-10081">

you could notice that the URL and the string literal is shown correctly except that the output shown for the string literal was not changed to & and instead shown as &amp; can someone help me to show it as & and not &amp;

标签: xml xslt
2条回答
疯言疯语
2楼-- · 2019-09-19 13:34

I have filled out my answer above. XML has a few reserved characters including '&' . XML uses it to create entity references. Thus

<a b="&foo;"/>

instructs the XML parser to find an enntity with name 'foo'. If you don't use the &...; construct you will get an error. Try reading

<a b="&src"/>

into any XML parser and it should throw an error. So the output you wish to create is illegal XML.

If you wish to create a NON-XML string, then you will have to output as text.

查看更多
在下西门庆
3楼-- · 2019-09-19 13:59

You can use D-O-E to generate unescaped content only as part of text nodes -- not in attributes.

http://w3.org/TR/1999/REC-xslt-19991116#disable-output-escaping :

"It is an error for output escaping to be disabled for a text node that is used for something other than a text node in the result tree. 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; "

查看更多
登录 后发表回答