Escaping of URL can be done from XSLT?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Escaping of URL can be done from XSLT?

  1. #1
    Gigi Guest

    Escaping of URL can be done from XSLT?


    I'm in a truoble trying to encode unsafe characters for URL directly from
    XSLT without using proprietary Microsoft extensions like the use of javascript
    functions, I have heard that It can be done with the use of recursive templates...
    if someone can help me, it could be of great help.

    Thanks in advance
    Gigi

  2. #2
    Russell Jones Guest

    Re: Escaping of URL can be done from XSLT?

    Here's one solution. Put the URL (the href value for the link) into a CDATA
    section. For example:

    Save this file as linkexample.xml (watch for extraneous line breaks):
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="linkexample.xsl"?>
    <root>
    <link>
    <linktext>My Link</linktext>
    <url>

    <![CDATA[http://myserver/mypath/somefile.asp?...am2=value2&par
    am3=value3]]>
    </url>
    </link>
    </root>

    Save this file as linkexample.xsl (watch for extraneous line breaks):
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xslutput method="html" omit-xml-declaration="yes"/>
    <xsl:template match="/">
    <xsl:apply-templates select="root/link"/>
    </xsl:template>
    <xsl:template match="link">
    <a href="{url}"><xsl:value-of select="linktext"/></a>
    </xsl:template>
    </xsl:stylesheet>

    Open the linkexample.xml file in IE. You'll see one link.

    Russell Jones
    Sr. Web Development Editor
    DevX.com
    "Gigi" <Gigigas@hotmail.com> wrote in message
    news:3a829e05$1@news.devx.com...
    >
    > I'm in a truoble trying to encode unsafe characters for URL directly from
    > XSLT without using proprietary Microsoft extensions like the use of

    javascript
    > functions, I have heard that It can be done with the use of recursive

    templates...
    > if someone can help me, it could be of great help.
    >
    > Thanks in advance
    > Gigi




  3. #3
    Jimmy Jack Guest

    Re: Escaping of URL can be done from XSLT?


    Your approach is fine if you have control over the XML source, but if you're
    concatenating strings to construct a URL (from a database perhaps), you can't
    guarantee that the resulting string will not contain unsafe characters:

    For example, with the following source XML and stylesheet (watch for line
    breaks):
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="linkexample.xsl"?>
    <clients>
    <client id="Chase Manhattan Bank" office="123 Main Street" />
    </clients>

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xslutput method="html" omit-xml-declaration="yes"/>
    <xsl:template match="/">
    <xsl:apply-templates select="clients/client"/>
    </xsl:template>
    <xsl:template match="client">
    <a href="http://www.myserver.com/process.asp?process={@id}"><xsl:value-of
    select="@id"/></a>
    </xsl:template>
    </xsl:stylesheet>

    Open in IE and you still see one link, but it is invalid because it contains
    spaces:

    <a href="http://www.myserver.com/process.asp?process=Chase Manhattan Bank">

    So back to the original question, how do I escape the id attribute in XSLT
    without JavaScript so that the href becomes:

    <a href="http://www.myserver.com/process.asp?process=Chase%20Manhattan%20Bank">

    Now IE will add the %20 to the link when I click it, but the problem becomes
    nasty if the id attribute contains the following characters ?, &, or /as
    IE will not convert them to %3F, %26, or %2F respectively.

    Thanks

    Jimmy

    "Russell Jones" <arj1@northstate.net> wrote:
    >Here's one solution. Put the URL (the href value for the link) into a CDATA
    >section. For example:
    >
    >Save this file as linkexample.xml (watch for extraneous line breaks):
    ><?xml version="1.0"?>
    ><?xml-stylesheet type="text/xsl" href="linkexample.xsl"?>
    ><root>
    > <link>
    > <linktext>My Link</linktext>
    > <url>
    >
    ><![CDATA[http://myserver/mypath/somefile.asp?...m2=value2&par
    >am3=value3]]>
    > </url>
    > </link>
    ></root>
    >
    >Save this file as linkexample.xsl (watch for extraneous line breaks):
    ><?xml version="1.0"?>
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >version="1.0">
    > <xslutput method="html" omit-xml-declaration="yes"/>
    > <xsl:template match="/">
    > <xsl:apply-templates select="root/link"/>
    > </xsl:template>
    > <xsl:template match="link">
    > <a href="{url}"><xsl:value-of select="linktext"/></a>
    > </xsl:template>
    ></xsl:stylesheet>
    >
    >Open the linkexample.xml file in IE. You'll see one link.
    >
    >Russell Jones
    >Sr. Web Development Editor
    >DevX.com
    >"Gigi" <Gigigas@hotmail.com> wrote in message
    >news:3a829e05$1@news.devx.com...
    >>
    >> I'm in a truoble trying to encode unsafe characters for URL directly from
    >> XSLT without using proprietary Microsoft extensions like the use of

    >javascript
    >> functions, I have heard that It can be done with the use of recursive

    >templates...
    >> if someone can help me, it could be of great help.
    >>
    >> Thanks in advance
    >> Gigi

    >
    >



  4. #4
    Russell Jones Guest

    Re: Escaping of URL can be done from XSLT?

    You can solve the embedded space problem by concatenating into a variable
    first.
    Try this (watch for line breaks)

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xslutput method="html" omit-xml-declaration="yes"/>
    <xsl:template match="/">
    <xsl:apply-templates select="clients/client"/>
    </xsl:template>
    <xsl:template match="client">
    <xsl:variable name="url">
    <xsl:value-of
    select="concat('http://www.myserver.com/process.asp?process={@id}', @id)"/>
    </xsl:variable>
    <a href="{$url}"><xsl:value-of select="@id"/></a>
    </xsl:template>
    </xsl:stylesheet>

    Russell Jones
    Sr. Web Development Editor
    DevX.com

    "Jimmy Jack" <xmldom@hotmail.com> wrote in message
    news:3a8ad512$1@news.devx.com...
    >
    > Your approach is fine if you have control over the XML source, but if

    you're
    > concatenating strings to construct a URL (from a database perhaps), you

    can't
    > guarantee that the resulting string will not contain unsafe characters:
    >
    > For example, with the following source XML and stylesheet (watch for line
    > breaks):
    > <?xml version="1.0"?>
    > <?xml-stylesheet type="text/xsl" href="linkexample.xsl"?>
    > <clients>
    > <client id="Chase Manhattan Bank" office="123 Main Street" />
    > </clients>
    >
    > <?xml version="1.0"?>
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    version="1.0">
    > <xslutput method="html" omit-xml-declaration="yes"/>
    > <xsl:template match="/">
    > <xsl:apply-templates select="clients/client"/>
    > </xsl:template>
    > <xsl:template match="client">
    > <a href="http://www.myserver.com/process.asp?process={@id}"><xsl:value-of
    > select="@id"/></a>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > Open in IE and you still see one link, but it is invalid because it

    contains
    > spaces:
    >
    > <a href="http://www.myserver.com/process.asp?process=Chase Manhattan

    Bank">
    >
    > So back to the original question, how do I escape the id attribute in XSLT
    > without JavaScript so that the href becomes:
    >
    > <a

    href="http://www.myserver.com/process.asp?process=Chase%20Manhattan%20Bank">
    >
    > Now IE will add the %20 to the link when I click it, but the problem

    becomes
    > nasty if the id attribute contains the following characters ?, &, or /as
    > IE will not convert them to %3F, %26, or %2F respectively.
    >
    > Thanks
    >
    > Jimmy
    >
    > "Russell Jones" <arj1@northstate.net> wrote:
    > >Here's one solution. Put the URL (the href value for the link) into a

    CDATA
    > >section. For example:
    > >
    > >Save this file as linkexample.xml (watch for extraneous line breaks):
    > ><?xml version="1.0"?>
    > ><?xml-stylesheet type="text/xsl" href="linkexample.xsl"?>
    > ><root>
    > > <link>
    > > <linktext>My Link</linktext>
    > > <url>
    > >
    > ><![CDATA[http://myserver/mypath/somefile.asp?...m2=value2&par
    > >am3=value3]]>
    > > </url>
    > > </link>
    > ></root>
    > >
    > >Save this file as linkexample.xsl (watch for extraneous line breaks):
    > ><?xml version="1.0"?>
    > > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > >version="1.0">
    > > <xslutput method="html" omit-xml-declaration="yes"/>
    > > <xsl:template match="/">
    > > <xsl:apply-templates select="root/link"/>
    > > </xsl:template>
    > > <xsl:template match="link">
    > > <a href="{url}"><xsl:value-of select="linktext"/></a>
    > > </xsl:template>
    > ></xsl:stylesheet>
    > >
    > >Open the linkexample.xml file in IE. You'll see one link.
    > >
    > >Russell Jones
    > >Sr. Web Development Editor
    > >DevX.com
    > >"Gigi" <Gigigas@hotmail.com> wrote in message
    > >news:3a829e05$1@news.devx.com...
    > >>
    > >> I'm in a truoble trying to encode unsafe characters for URL directly

    from
    > >> XSLT without using proprietary Microsoft extensions like the use of

    > >javascript
    > >> functions, I have heard that It can be done with the use of recursive

    > >templates...
    > >> if someone can help me, it could be of great help.
    > >>
    > >> Thanks in advance
    > >> Gigi

    > >
    > >

    >




  5. #5
    don Guest

    Re: Escaping of URL can be done from XSLT?


    This is what's most aggravating about "new technology" forums... Here's a
    detailed question, with a single line reply that only vaguely passes as an
    "answer". (A) no, you don't SEE "one link",(B) no, it does not deal with
    "encoding unsafe characters" and (C) this looks more like an "XSL" file than
    an "XML" so why save it with the latter file type?

    More questions than answers.

    Here, by the way, is what you "see" after opening this file in IE5:

    <?xml version="1.0" ?>
    - <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xslutput method="html" omit-xml-declaration="yes" />
    - <xsl:template match="/">
    <xsl:apply-templates select="root/link" />
    </xsl:template>
    - <xsl:template match="link">
    - <a href="{url}">
    <xsl:value-of select="linktext" />
    </a>
    </xsl:template>
    </xsl:stylesheet>

    "Russell Jones" <arj1@northstate.net> wrote:
    >Here's one solution. Put the URL (the href value for the link) into a CDATA
    >section. For example:
    >
    >Save this file as linkexample.xml (watch for extraneous line breaks):
    ><?xml version="1.0"?>
    ><?xml-stylesheet type="text/xsl" href="linkexample.xsl"?>
    ><root>
    > <link>
    > <linktext>My Link</linktext>
    > <url>
    >
    ><![CDATA[http://myserver/mypath/somefile.asp?...m2=value2&par
    >am3=value3]]>
    > </url>
    > </link>
    ></root>
    >
    >Save this file as linkexample.xsl (watch for extraneous line breaks):
    ><?xml version="1.0"?>
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >version="1.0">
    > <xslutput method="html" omit-xml-declaration="yes"/>
    > <xsl:template match="/">
    > <xsl:apply-templates select="root/link"/>
    > </xsl:template>
    > <xsl:template match="link">
    > <a href="{url}"><xsl:value-of select="linktext"/></a>
    > </xsl:template>
    ></xsl:stylesheet>
    >
    >Open the linkexample.xml file in IE. You'll see one link.
    >
    >Russell Jones
    >Sr. Web Development Editor
    >DevX.com
    >"Gigi" <Gigigas@hotmail.com> wrote in message
    >news:3a829e05$1@news.devx.com...
    >>
    >> I'm in a truoble trying to encode unsafe characters for URL directly from
    >> XSLT without using proprietary Microsoft extensions like the use of

    >javascript
    >> functions, I have heard that It can be done with the use of recursive

    >templates...
    >> if someone can help me, it could be of great help.
    >>
    >> Thanks in advance
    >> Gigi

    >
    >



  6. #6
    Russell Jones Guest

    Re: Escaping of URL can be done from XSLT?

    You need to read the whole thread. My second post built on my initial
    answer, which contains the XML file. Also, you need to be running the
    release version of MSXML3.dll in replace mode (run xmlinst.exe) to see the
    output correctly. I usually mention that, but omitted it on this reply.

    "don" <dbweb4z@premiersi.com> wrote in message
    news:3a9553de$1@news.devx.com...
    >
    > This is what's most aggravating about "new technology" forums... Here's a
    > detailed question, with a single line reply that only vaguely passes as an
    > "answer". (A) no, you don't SEE "one link",(B) no, it does not deal with
    > "encoding unsafe characters" and (C) this looks more like an "XSL" file

    than
    > an "XML" so why save it with the latter file type?
    >
    > More questions than answers.
    >
    > Here, by the way, is what you "see" after opening this file in IE5:
    >
    > <?xml version="1.0" ?>
    > - <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    version="1.0">
    > <xslutput method="html" omit-xml-declaration="yes" />
    > - <xsl:template match="/">
    > <xsl:apply-templates select="root/link" />
    > </xsl:template>
    > - <xsl:template match="link">
    > - <a href="{url}">
    > <xsl:value-of select="linktext" />
    > </a>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > "Russell Jones" <arj1@northstate.net> wrote:
    > >Here's one solution. Put the URL (the href value for the link) into a

    CDATA
    > >section. For example:
    > >
    > >Save this file as linkexample.xml (watch for extraneous line breaks):
    > ><?xml version="1.0"?>
    > ><?xml-stylesheet type="text/xsl" href="linkexample.xsl"?>
    > ><root>
    > > <link>
    > > <linktext>My Link</linktext>
    > > <url>
    > >
    > ><![CDATA[http://myserver/mypath/somefile.asp?...m2=value2&par
    > >am3=value3]]>
    > > </url>
    > > </link>
    > ></root>
    > >
    > >Save this file as linkexample.xsl (watch for extraneous line breaks):
    > ><?xml version="1.0"?>
    > > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > >version="1.0">
    > > <xslutput method="html" omit-xml-declaration="yes"/>
    > > <xsl:template match="/">
    > > <xsl:apply-templates select="root/link"/>
    > > </xsl:template>
    > > <xsl:template match="link">
    > > <a href="{url}"><xsl:value-of select="linktext"/></a>
    > > </xsl:template>
    > ></xsl:stylesheet>
    > >
    > >Open the linkexample.xml file in IE. You'll see one link.
    > >
    > >Russell Jones
    > >Sr. Web Development Editor
    > >DevX.com
    > >"Gigi" <Gigigas@hotmail.com> wrote in message
    > >news:3a829e05$1@news.devx.com...
    > >>
    > >> I'm in a truoble trying to encode unsafe characters for URL directly

    from
    > >> XSLT without using proprietary Microsoft extensions like the use of

    > >javascript
    > >> functions, I have heard that It can be done with the use of recursive

    > >templates...
    > >> if someone can help me, it could be of great help.
    > >>
    > >> Thanks in advance
    > >> Gigi

    > >
    > >

    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center