XSLT to write links
-
Given the following xml:
<para>The <link url="earth.htm">earth</link> rotates.</para>
Is there a way to use XSLT to write the content of the <para> element like this:
<p>The <a href="earth.htm">earth</a> rotates.</p> -
Given the following xml:
<para>The <link url="earth.htm">earth</link> rotates.</para>
Is there a way to use XSLT to write the content of the <para> element like this:
<p>The <a href="earth.htm">earth</a> rotates.</p>Something along the lines of:
<xsl:template match="link">
<a>
<xsl:attribute name="href"><xsl:value-of select="@url" /></xsl:attribute>
<xsl:value-of select="."/>
</a>
</xsl:template>I think. Its been a while since I have done XSLT. -- David Wengier Sonork ID: 100.14177 - Ch00k
-
Something along the lines of:
<xsl:template match="link">
<a>
<xsl:attribute name="href"><xsl:value-of select="@url" /></xsl:attribute>
<xsl:value-of select="."/>
</a>
</xsl:template>I think. Its been a while since I have done XSLT. -- David Wengier Sonork ID: 100.14177 - Ch00k
That part works if you only want to show links. What I was wondering was if there was a simple way to put the link inside the rest of the text, i.e. in my example the result I wanted was
The earth rotates. -
Given the following xml:
<para>The <link url="earth.htm">earth</link> rotates.</para>
Is there a way to use XSLT to write the content of the <para> element like this:
<p>The <a href="earth.htm">earth</a> rotates.</p>MarSCoZa wrote: The earth rotates. Technically that kind of XML is not really valid. e.g. <para> The <link url="earth.htm">earth</link> rotates. </para> The
The
andRotates
PCDATA sections are "floating". The reason being that an element can either only contain PCDATA or child-elements. It cannot contain both. Think about creating a DTD which defines that... You cannot really. The DTD element definition cannot contain PCDATA and an elements name. However XML is quite forgiving in this case (which is strange considering it's normally very unforgiving nature) and you can use the following XSL to transform it.<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template><xsl:template match="link"> <a> <xsl:attribute name="href"> <xsl:value-of select="@url" /> </xsl:attribute> <xsl:value-of select="."/> </a> </xsl:template>
</xsl:stylesheet>
The key is
templates
. I used to hate them and thought they were these daft, never used bits of XSL. Until I figured out how they worked and went "oooohh yes!" :) Enjoy! regards, Paul Watson Bluegrass Cape Town, South Africa The greatest thing you'll ever learn is just to love, and to be loved in return - Moulin Rouge -
MarSCoZa wrote: The earth rotates. Technically that kind of XML is not really valid. e.g. <para> The <link url="earth.htm">earth</link> rotates. </para> The
The
andRotates
PCDATA sections are "floating". The reason being that an element can either only contain PCDATA or child-elements. It cannot contain both. Think about creating a DTD which defines that... You cannot really. The DTD element definition cannot contain PCDATA and an elements name. However XML is quite forgiving in this case (which is strange considering it's normally very unforgiving nature) and you can use the following XSL to transform it.<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template><xsl:template match="link"> <a> <xsl:attribute name="href"> <xsl:value-of select="@url" /> </xsl:attribute> <xsl:value-of select="."/> </a> </xsl:template>
</xsl:stylesheet>
The key is
templates
. I used to hate them and thought they were these daft, never used bits of XSL. Until I figured out how they worked and went "oooohh yes!" :) Enjoy! regards, Paul Watson Bluegrass Cape Town, South Africa The greatest thing you'll ever learn is just to love, and to be loved in return - Moulin RougePaul Watson wrote: The key is templates. I used to hate them and thought they were these daft, never used bits of XSL. Until I figured out how they worked and went "oooohh yes!" :omg: :omg: I've never used XSLT for XML->HTML, but with XML->XML, templates are the god you bow down to and pray. It boggles my mind the thought of NOT using template matching in XSLT.:omg: :omg: