XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).
Moderator: General Moderators
Nathaniel
Forum Contributor
Posts: 396 Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA
Post
by Nathaniel » Wed Aug 02, 2006 4:01 pm
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
kendall
Forum Regular
Posts: 852 Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:
Post
by kendall » Wed Aug 02, 2006 4:13 pm
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>
Nathaniel
Forum Contributor
Posts: 396 Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA
Post
by Nathaniel » Wed Aug 02, 2006 4:16 pm
Right, got that much. Thanks.
How do I apply templates to inline markup though?
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Aug 02, 2006 5:06 pm
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.
Nathaniel
Forum Contributor
Posts: 396 Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA
Post
by Nathaniel » Wed Aug 02, 2006 5:39 pm
Yeah, I suppose that happening would be an acceptable result.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Aug 02, 2006 5:47 pm
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.
shoebappa
Forum Contributor
Posts: 158 Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA
Post
by shoebappa » Wed Aug 02, 2006 6:08 pm
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.
shoebappa
Forum Contributor
Posts: 158 Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA
Post
by shoebappa » Wed Aug 02, 2006 6:13 pm
There's also xpath for matching text nodes "text()"
Nathaniel
Forum Contributor
Posts: 396 Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA
Post
by Nathaniel » Thu Aug 03, 2006 9:56 am
Wow, thanks man. That works swell.