Reputation: 355
Here is my example xml example :
<bloc id = "mybloc">
<ref_ex ref = "data1"/>
<ref_ex ref = "data2"/>
</bloc>
<ex id = "data1">
<name>Hello</name>
.. (something else)
</ex>
<ex id = "data2">
<name>Hello Me !</name>
.. (something else)
</ex>
I would like to get this html code like :
<a href="#data1">Hello</a>
<a href="#data2">Hello Me!</a>
I've tried with this xsl :
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
<xsl:output method = "html"/>
<xsl:template match = "ref_ex">
<a href = "#{@ref}">
<xsl:template match = "ex" use="@ref">
<xsl:value-of select = "@name"/>
</xsl:template>
</a>
</xsl:template>
</xsl:stylesheet>
but I get this error :
element template only allowed as child of stylesheet !
someone have any idea to do that ? by respecting my format.
Upvotes: 0
Views: 140
Reputation: 22647
From the comments section: You simply cannot use xsl:template
inside another xsl:template
. Why? Because it makes awful little sense. I suggest you read about the basics of XSLT in order to understand the concept of tempate matches.
The stylesheet below transforms ex
elements into a
elements if their ID is referenced in a bloc
element. Another viable solution, as suggested by @helderarocha, would be to use keys.
Assuming correct input (a root element to make it well-formed):
<root>
<bloc id = "mybloc">
<ref_ex ref = "data1"/>
<ref_ex ref = "data2"/>
</bloc>
<ex id = "data1">
<name>Hello</name>
<!--.. (something else)-->
</ex>
<ex id = "data2">
<name>Hello Me !</name>
<!--.. (something else)-->
</ex>
</root>
Stylesheet
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="bloc[@id='mybloc']">
<xsl:apply-templates select="//ex[id = current()/ref_ex/@ref]"/>
</xsl:template>
<xsl:template match="ex">
<a href="{@id}">
<xsl:value-of select="."/>
</a>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a href="data1">Hello</a>
<a href="data2">Hello Me !</a>
</root>
Upvotes: 1