How do I apply an XSL template to inline markup?

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

How do I apply an XSL template to inline markup?

Post 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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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>
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Right, got that much. Thanks.

How do I apply templates to inline markup though?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

So are you talking about how to get 'I agree to the' and 'and' to show up?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Yeah, I suppose that happening would be an acceptable result.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Now that I think of it, isn't that malformed XML? Can you have text & elements bumbing against each other like that?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post 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>.
Last edited by shoebappa on Thu Aug 03, 2006 8:25 am, edited 1 time in total.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

There's also xpath for matching text nodes "text()"
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Wow, thanks man. That works swell.
Post Reply