You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by "Carwyn T. Edwards" <ca...@hsl.uk.com> on 2001/02/14 12:32:23 UTC
Generating Paths from XSLT
This question kind of follows on from my XPath generator question:
If I have a document something like:
<example>
<subnode>
<changeme>3</changeme>
</subnode>
</example>
.. then convert it into a HTML document with a form in it ...
<html>
...
<form action="http://myservlet" .. >
<input type="text" name="path-to-changeme-node">
<input type="submit">
</form>
...
</html>
Is it possible to use XSLT to generate a valid XPath string for the
value of the name attribute above (path-to-changeme-node).
It looks vaguely possible with some xsl:number elements, some
position()s and some way of getting all the ancestor nodeNames.
Another use for something like this would be printing the full section
name nesting at the top of a page. (MyBook > Chapter 1 > Section 3).
Carwyn
RE: Generating Paths from XSLT
Posted by Jarno Elovirta <ja...@codeonline.com>.
> If I have a document something like:
>
> <example>
> <subnode>
> <changeme>3</changeme>
> </subnode>
> </example>
>
> .. then convert it into a HTML document with a form in it ...
>
> <html>
> ...
> <form action="http://myservlet" .. >
> <input type="text" name="path-to-changeme-node">
> <input type="submit">
> </form>
> ...
> </html>
>
> Is it possible to use XSLT to generate a valid XPath string for the
> value of the name attribute above (path-to-changeme-node).
Something like this, perhaps?
[c:\temp]type test.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<example>
<subnode>
<changeme>3</changeme>
</subnode>
</example>
[c:\temp]type test.xsl
<?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" />
<xsl:template match="changeme">
<form action="http://myservlet" >
<input type="text">
<xsl:attribute name="name">
<xsl:call-template name="resolver" />
</xsl:attribute>
</input>
<input type="submit" />
</form>
</xsl:template>
<xsl:template name="resolver" >
<xsl:for-each select="ancestor-or-self::*">
<xsl:variable name="id" select="generate-id(.)" />
<xsl:variable name="name" select="name()" />
<xsl:value-of select="concat('/',name())"/>
<xsl:for-each select="../*[name() = $name]" >
<xsl:if test="generate-id(.) = $id">
<xsl:text/>[<xsl:value-of select="position()"/>]<xsl:text/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<xsl:if test="not(self::*)">
<xsl:value-of select="concat('/@',name())" />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
[c:\temp]xalan2-j -in test.xml -xsl test.xsl
<form action="http://myservlet">
<input type="text" name="/example[1]/subnode[1]/changeme[1]"><input
type="submit">
</form>
There are probably better ways of doing this.
Cheers,
Jarno
--
"We are the Borg. Lower you shields and surrender your ships. We will add
your biological and technological distinctiveness to our own. Your culture
will adapt to service us. Resistance is futile."