Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - XSLT variable problem [Thread Next] Re: XSLT variable problemTo: NULL Date: 10/3/2005 9:43:00 AM Under XSLT 2.0 the value of a variable declared like this: <xsl:variable name=3D"myNumbers"> > <xsl:for-each select=3D"//interestingstuff"> > <xsl:sort select=3D"./@number" data-type=3D"number" > order=3D"ascending" /> > <xsl:copy-of select=3D"."/> > </xsl:for-each> > </xsl:variable> is a temporary tree: a document node owning zero or more interestingStuff elements copied from the source document and sorted. When you then do $myNumbers[1]/@number you are trying to select an attribute of a document node, but document nodes do not have attributes. To get the effect you want, add as=3D"element()*" to the xsl:variable - the value will then be a sequence of element nodes. You could also use xsl:sequence in place of xsl:copy-of to avoid the unnecessary copying of the elements. If this "works" as written in Altova, then Altova has a bug. Michael Kay Jon Martin Solaas wrote: > Hi, > > I have a general document somewhat like this: > -------------------------------------- > <root> > <level1> > <level2> > <interestingstuff number=3D"2"/> > <interestingstuff number=3D"3"/> > <interestingstuff number=3D"1"/> > </level2> > <level2> > <interestingstuff number=3D"10"/> > </level2> > </level1> > <level1> > <interestingstuff number =3D "11"/> > </level1> > </root> > -------------------------------------- > > Now, I want to use the lowest number value from the interestingstuff > nodes' number attribute in various places in an xslt transformation. > > My xslt looks like this: > -------------------------------------- > <?xml version=3D"1.0" encoding=3D"ISO-8859-1"?> > <xsl:stylesheet > version=3D"2.0" > xmlns:xsl=3D"http://www.w3.org/1999/XSL/Transform" > xmlns:xs=3D"http://www.w3.org/2001/XMLSchema" > xmlns:fn=3D"http://www.w3.org/2005/02/xpath-functions" > xmlns:xst=3D"http://www.w3.org/2005/02/xpath-datatypes" > exclude-result-prefixes=3D"xs fn xdt"> > > <xsl:output > method=3D"xml" > version=3D"1.0" > encoding=3D"ISO-8859-1" > indent=3D"yes"/> > > <xsl:template match=3D"/"> > > <xsl:variable name=3D"myNumbers"> > <xsl:for-each select=3D"//interestingstuff"> > <xsl:sort select=3D"./@number" data-type=3D"number" > order=3D"ascending" /> > <xsl:copy-of select=3D"."/> > </xsl:for-each> > </xsl:variable> > > <xsl:for-each select=3D'root'> > <MyElement> > <xsl:attribute name=3D"TimeStamp"> > <xsl:value-of select=3D"$myNumbers[1]/@number"/> > </xsl:attribute> > </MyElement> > </xsl:for-each> > </xsl:template> > </xsl:stylesheet> > -------------------------------------- > > The idea is to store a sorted list of interestingstuff-nodes, that may > occure "anywehre" in the xml, in the myNumbers variable, and then just > pick the first one to get hold of the lowest number. Running in Altova > XMLSpy">XMLSpy this works, but not with Oracle XML or Stylus Studio/Saxon, so I > suppose the Altova processor is a bit too forgiving and I'm doing > something illegal. But what? > > Also, as I don't need the whole sorted list of numbers, just the lowest > one, I should find a way to store only that number in the variable. I > suppose I'll figure that one out myself, it's spotting the error in the > stylesheet above I just can't seem to grasp, and I really would like to > understand what's wrong. >=20 > --=20 > jon martin solaas > jon_martin_solaas =A4 y-a-h-o-o...n-o | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
