Escaping <a href> in XML


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.


<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 < 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,

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?


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 < or > 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,

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?


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

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:

<link dest="test.htm">a link</link>

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

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

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.

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:

	<link dest="test.htm">a link</link>

In the XSLT template you can put the attribute in {}. eg:

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

Many thanks again for the help,