SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Escaping <a href> in XML

    Hi,

    I've recently bought the Sitepoint book No Nonsense XML Web Development With PHP, and have tried to apply the knowledge to one of my ownsites but I'm getting tripped up.

    I have successfully created a homepage to pull its content from an XML file but within this file I have <bodyCopy> tag that I need to contain a link but I can't get the link to display.

    eg:

    Code XML:
    <bodyCopy>Here is some text that includes <a href="test.htm">a link</a> and some more text</bodyCopy>

    I can't get the link to work, I've tried escaping the < to &lt; and using CDATA. I've also tried using <link> and using XSLT to transform to <a href=""> but I can't get the XSLT to recognise the link.

    Any suggestions, I'm sure its something obvious but I just can't see it!
    Many thanks in advance,
    D

  2. #2
    SitePoint Addict ekompute's Avatar
    Join Date
    Dec 2008
    Location
    Malaysia
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Belfast75 View Post
    Code XML:
    <bodyCopy>Here is some text that includes <a href="test.htm">a link</a> and some more text</bodyCopy>

    I can't get the link to work, I've tried escaping the < to &lt; and using CDATA. I've also tried using <link> and using XSLT to transform to <a href=""> but I can't get the XSLT to recognise the link.

    Any suggestions, I'm sure its something obvious but I just can't see it!
    Many thanks in advance,
    D
    I am no expert but I do tinker with my php files on and off. Did you close the php portion of the file with the ending ?php> tag before you start your XML codes?

  3. #3
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    No, sorry I should have explained the error better. The text is still appearing before and after the <a href> eg if I set the XML as highlights in my original post. Then the text on screen displays as

    'Here is some text that includes and some more text'

    Or if try to enclose it as CDATA or escape the < or > as &lt; or &gt; then it displays as

    'Here is some text that includes <a href="test.htm">a link</a> and some more text'

    Which is sort of what I'd expect it to do but I don't know what I need to do to get it to display as a link.

    Thanks anyway,
    D

  4. #4
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait - so you're expecting this markup, which is not XHTML, HTML to be rendered as an HTML link? Huh? What's the document type? Got a link to the page?
    Cross browser css bugs

    Dan Schulz you will be missed

  5. #5
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    No, you're right SoulScratch. That would be some what silly. I'm displaying the XML through php eg:

    Code PHP:
    echo '<p>' . htmlentities($homePage->mainCopy) . '</p>';

    but yes with more thought it only displays the XML, rather than translating it into HTML.

    I was looking at creating a <link> in the XML and using XSLT to translate it eg:

    Code XML:
    <link dest="test.htm">a link</link>

    but I can't get the values into the template eg:

    Code XML:
    	<xsl:template match="link">
    		<a href="@dest"><xsl:apply-templates/></a>
    	</xsl:template>

    Obviously this doesn't work so I suppose my question should have been 'how do I get values into the template?'

    Thanks again for the help.

  6. #6
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've cracked it. Simple really, like most things, just need to know the syntax!

    For anyone who stumbles across this post in future, here is the solution and a big nod and link to the article I read.

    The XML:
    Code XML:
    	<link dest="test.htm">a link</link>

    In the XSLT template you can put the attribute in {}. eg:
    Code XML:
    	<xsl:template match="link">
    		<a href="{@dest}"><xsl:apply-templates/></a>
    	</xsl:template>

    Many thanks again for the help,
    D


Tags for this Thread

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
  •