This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Getting & to convert to %26 in URI


drcambron wrote:
> I've been following this list and learning.  I'm now stuck on something.  I
> have some XML data I am using with the production released MSXML3 in IE5 to
> build a URI in HTML. An ID element in the XML may have '&' chars in it and I
> can't seem to find the way to get it properly escaped.  For example the
> actual data may look like:
> 
> MARK & SCAN
> 
> and the XML for the ID element that I send to the browser looks like:
> 
> <ID>MARK&#x20;&#x26;&#x20;SCAN</ID>

...when this XML is parsed, it becomes the character data MARK & SCAN.
That's all the XSLT processor will get to see. Your character references
are just to disambiguate the character data from the markup, for the
parser's benefit. You weren't actually making the character data be
MARK&#x20;&#x26;&#x20;SCAN.

> In the XSL code, I simply try to build the URI as follows (not everything is
> here...)
> 
> <a>
>   <xsl:attribute name="href">
>     <xsl:value-of select="$Server"/>?ID=<xsl:value-of select="ID"/>&amp;APP=
> etc...
>   </xsl:attribute>                  
>   <xsl:value-of select="ID"/>
> </a> 

Probably easier to do something like
 <xsl:value-of select="concat($Server,'?ID=',ID,'&amp;APP=',etc)"/>
or
 <a href="{$Server}?ID={ID}&amp;APP={etc}">click here</a>

> I have <xsl:output method="html"/> set, and when I copy the link into the
> address field on the browser, I see that the spaces have converted, but not
> the & char.  The URI will look like:
> 
> http://server?ID=A%20&%20B&APP= etc... with the & still explicity there
> hosing my URI.  I'm sure I'm missing something simple.

You are getting exactly what you asked for, but...

  - what you see in the status bar when you mouse over the link in IE
    is the URI derived from what text & markup is actually in the
    HTML. i.e., &amp; in the document will become & in the URI.

    if you want to see the actual HTML that was generated, use the 'view
    XSL output' context menu addition that you can download from MS.
    There's a link to it in the MSXML FAQ.

    my testing indicates that the href comes out exactly as expected:
    <a href="http://www.skew.org/printenv.cgi?ID=MARK &amp; SCAN&amp;APP=foo">

  - what you asked for is not what you wanted, because to be a proper URI
    the '&' in the first parameter must be '%26' and the spaces must be
    either '%20' or '+'. you didn't ask for these, so that's why you
    didn't get them.

Your real problem is that URL encoding is not a standard feature of
XSLT/XPath. On Nov 17 I posted a way to use Java's built-in URL encoder
via an extension function. That may not do you any good if you're using
MSXML. Anyone have any tips for doing URL encoding with MSXML?

If you are reasonably sure about your input data, you could probably get
away with just escaping a few characters that you expect to see, by using
translate() to convert spaces to plus signs, and/or something like
http://www.skew.org/xml/stylesheets/replace/replace.xsl for the rest.

   - Mike
____________________________________________________________________
Mike J. Brown, software engineer at         My XML/XSL resources:
webb.net in Denver, Colorado, USA           http://www.skew.org/xml/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]