[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Accessing xml node data using variable


Subject: Re: [xsl] Accessing xml node data using variable
From: "Sanket Pattekar" <sanket.pattekar@xxxxxxxxx>
Date: Sat, 22 Apr 2006 17:27:17 +0200

Thanks Michael for the solution. The example given here is cooked up,
but we wanted to use this in our live situation. Hence we cannot
change the design.

This XPath expression has solved our case. Thanks again.

Sanket

On 4/22/06, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> Firstly, packing information into structured element names is bad design.
> This is what attributes are for. Don't use <title-en> and <title-nl>, use
> <title xml:lang="en"> and <title xml:lang="nl">.
>
> Secondly, this doesn't work:
>
> <xsl:value-of select="root/{$MyTitleName}"/>
>
> You can't use curly braces inside an XPath expression, and you can't
> construct XPath expressions dynamically from strings. You want
>
> <xsl:value-of select="root/*[name() = $MyTitleName]"/>
>
> or if you change the design,
>
> root/title[@xml-lang=$lang]
>
> Michael Kay
> http://www.saxonica.com/
>
> > -----Original Message-----
> > From: Sanket Pattekar [mailto:sanket.pattekar@xxxxxxxxx]
> > Sent: 22 April 2006 10:11
> > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > Subject: [xsl] Accessing xml node data using variable
> >
> > Hi
> >
> > I have a xml file called hello.xml that looks like <?xml
> > version="1.0"?> <?xml-stylesheet type="text/xsl"
> > href="hello.xsl"?> <root> <greeting>Hello world.</greeting>
> > <title-en>News Title English</title-en> <title-nl>News Title
> > Dutch</title-nl> </root>
> >
> > Then I have the stylesheet called hello.xsl that looks like
> > <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet
> > version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> >       <xsl:variable name="lang" select="'en'" />
> >       <xsl:variable name="MyTitleName"
> > select="concat('title-',$lang)"/>
> >
> >       <xsl:template match="/">
> >               <head><title>Greeting</title></head>
> >                       <body>
> >                       <p>Words of greeting:<br/>
> >                               <b><i><u><xsl:value-of
> > select="root/greeting"/></u></i></b><br/>
> >                               <b><i><u><xsl:value-of
> > select="root/title-en"/></u></i></b><br/>
> >                               <b><i><u><xsl:value-of
> > select="root/title-nl"/></u></i></b><br/>
> >                               <b><i><u><xsl:value-of
> > select="root/{$MyTitleName}"/></u></i></b><br/>
> >                        </p>
> >                       </body>
> >       </xsl:template>
> > </xsl:stylesheet>
> >
> > Now I want to create a variable called 'MyTitleName' in the
> > stylesheet, that refrences my xml node. Any suggestion would
> > be welcome.
> >
> > Sanket


Current Thread
Keywords