Page 1 of 1

How do I apply an XSL template to inline markup?

Posted: Wed Aug 02, 2006 4:01 pm
by Nathaniel
Hi peoples.

Code: Select all

<label>I agree to the 
	<link url="/terms/">terms of use</link> and 
	<link url="/privacy/">privacy policy</link>.
</label>
How do I get XSL to take the label element and turn it into

Code: Select all

I agree to the <a href="/terms/">terms of use</a> and <a href="/privacy/">privacy policy</a>.
It's simple enough with strings that don't have inline markup, but I've been googling and I'm clueless here. The best I could get it to do was make

Code: Select all

<a href="/terms/">terms of use</a><a href="/privacy/">privacy policy</a>
Cheerio!
- Nathaniel

Posted: Wed Aug 02, 2006 4:13 pm
by kendall
Yo dude,

your XSL syntax should be something like

Code: Select all

<a>
<xsl:attribute name="href">
<xsl:value-of select="label/link/@url"/>
</xsl:attribute>
<xsl:value-of select = "label/link"/>
</a>

Posted: Wed Aug 02, 2006 4:16 pm
by Nathaniel
Right, got that much. Thanks.

How do I apply templates to inline markup though?

Posted: Wed Aug 02, 2006 5:06 pm
by pickle
So are you talking about how to get 'I agree to the' and 'and' to show up?

Posted: Wed Aug 02, 2006 5:39 pm
by Nathaniel
Yeah, I suppose that happening would be an acceptable result.

Posted: Wed Aug 02, 2006 5:47 pm
by pickle
Now that I think of it, isn't that malformed XML? Can you have text & elements bumbing against each other like that?

Posted: Wed Aug 02, 2006 6:08 pm
by shoebappa
Definately valid XML. Only way I know to match text nodes thrown about in no particular structure is to rely on <xsl:apply-templates /> and the fact that you have no templates matching text nodes so it just spits out the text right inline.

Example (tested with your example):

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1" indent="yes" omit-xml-declaration="yes"  media-type="text/html"/>

<xsl:template match="/">
	<xsl:apply-templates />
</xsl:template>

<xsl:template match="label">
	<xsl:apply-templates />
</xsl:template>

<xsl:template match="link">
	<a>
		<xsl:attribute name="href">
			<xsl:value-of select="@url"></xsl:value-of>
		</xsl:attribute>
		<xsl:value-of select="."></xsl:value-of>
	</a>
</xsl:template>

</xsl:stylesheet>
Output:

Code: Select all

I agree to the 
	<a href="/terms/">terms of use</a> and 
	<a href="/privacy/">privacy policy</a>.

Posted: Wed Aug 02, 2006 6:13 pm
by shoebappa
There's also xpath for matching text nodes "text()"

Posted: Thu Aug 03, 2006 9:56 am
by Nathaniel
Wow, thanks man. That works swell.