Using Xsl: value-of - Converting & in url to & amp;

I have xml something likethis

<root> <testxml> <details name="test" url="http://www.test.com/test.aspx?val=100&val2=200" /> </testxml> <root> 

When I convert this xml using xslt to another xml

 <xsl:output method="html" indent="yes" encoding="UTF-8" /> <xsl:template match="root/testxml/details"> <convert> <xsl:attribute name="url"> <xsl:value-of select="@url"/> </xsl:attribute> </convert> 

I get output like this

 <convert url="http://www.test.com/test.aspx?val=100&amp;val2=200"> 

instead

 <convert url="http://www.test.com/test.aspx?val=100&val2=200"> 

the problem is here: both in the url are changing and , as I can avoid this, I want in the url as &. (not & amp;)

I tried <xsl:value-of disable-output-escaping="yes" select="@url" /> as well, but not used.

Can someone please help me with this.

+4
source share
3 answers

I get output like this

instead

the problem is here: in the url it turns out changed to and

No problem, and the URL does not change at all :

To verify this, use the following conversion:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="/*"> <xsl:value-of select="@url"/> </xsl:template> </xsl:stylesheet> 

when this conversion is applied to the following XML document:

 <convert url="http://www.test.com/test.aspx?val=100&amp;val2=200"/> 

Result :

 http://www.test.com/test.aspx?val=100&val2=200 

So, the URL has not been changed in any way, and there is only one & character left in it.

What you are observing is the mandatory escaping of some special characters (usually < and & ) in XML, as dictated by the XML specification.

Removing some special characters never changes the contents of a string that is escaped - just what it looks like when it is part of an XML document.

how can i avoid this i want in url like & (not & amp;)

You cannot, and as shown above, nothing can be avoided.

+4
source

& must be escaped as & in the url in the xml document. You cannot have unlimited ampersand.

+1
source

Dimitre is quite correct - an ampersand in XML should always be escaped, and it’s not clear why you are trying so hard to prevent it.

I am puzzled, but you say that you generate XML, but you use the HTML output method, but you generate a <convert> element that is not defined in HTML. Therefore, I am afraid that you are somewhat embarrassed.

+1
source

Source: https://habr.com/ru/post/1347093/


All Articles