SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xml/xsl and links

    i'm transforming some xml with an xslt stylesheet (using C#/ASP.Net) into html. here is a sample of the xml:
    Code:
    <?xml version="1.0" encoding="utf-8" ?> 
    <content>
    	<article>This is content with a link, &lt;a href="http://www.mylink.com"&gt;click here&lt;/a&gt;.</article>
    </content>
    the link are doesn't display as a link but as code in the resulting html page. what do i need to do to make an actual link once it's written out to the page?

    i've also tried character code 60 & 62 with the same result...

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    This worked for me:

    XML
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <?xml-stylesheet href="content.xsl" type="text/xsl"?>
    <content>
    	<article>This is content with a link, 
            <a href="http://www.mylink.com">click here</a>.
            </article>
    </content>
    XSL
    HTML Code:
    <?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="html" version="4.01" encoding="utf-8" />
      <xsl:template match="/">
      <html>
      <head>
      <title>Transformation!</title>
      </head>
      <body>
      <p><xsl:copy-of select="content/article" /></p>
      </body>
      </html>
      </xsl:template>
    </xsl:stylesheet>
    No need to encode the less-than and greater-than signs .

  3. #3
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i didn't even think about doing that. it almost doesn't look well formed. hmmmm. will have to investigate some. thanks vinnie...

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by bbolte
    i didn't even think about doing that. it almost doesn't look well formed. hmmmm. will have to investigate some. thanks vinnie...
    The XML is well-formed, but it may or may not validate depending on what you have in your DTD or schema (if you have one at all, which you should ). If you have a DTD/schema and the <a> element isn't in it, your XML file won't validate.

    One thing you should know with this approach though is that the <a> element can be treated just like any other element/node in your XML file (meaning it can be accessed by XPath, etc). If you don't want this to happen, wrap the contents of your <article> element in a CDATA section, like so:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <?xml-stylesheet href="content.xsl" type="text/xsl"?>
    <content xmlns:html="http://www.w3.org/1999/xhtml">
    	<article>
            <![CDATA[
            This is content with a link, <a href="http://www.mylink.com">click here</a>.
            ]]>
            </article>
    </content>
    If you wrap it in a CDATA section you have to alter the stylesheet a little bit and use value-of instead of copy-of (since you don't want a copy of everything, you just want the value of the CDATA section). You also have to disable output escaping so the angle brackets of the links don't get turned into the encoded equivalents (& gt; and & lt; ) Here's the stylesheet that worked for me:
    HTML Code:
    <?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet 
    	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    	version="1.0">
      <xsl:output method="html" version="4.01" encoding="utf-8" />
      <xsl:template match="/">
      <html>
      <head>
      <title>Transformation!</title>
      </head>
      <body>
      <p><xsl:value-of disable-output-escaping="yes" select="content/article" /></p>
      </body>
      </html>
      </xsl:template>
    </xsl:stylesheet>
    I think this method will work best actually, as opposed to the previous method, because it keeps your article content from getting mixed up in your XML data, and if you decide you want to use a DTD or Schema in the future you don't have to add any HTML elements into it. Hope this helps!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •