473,375 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 software developers and data experts.

XSLT variable problem

Hi,

I have a general document somewhat like this:
--------------------------------------
<root>
<level1>
<level2>
<interestingstuff number="2"/>
<interestingstuff number="3"/>
<interestingstuff number="1"/>
</level2>
<level2>
<interestingstuff number="10"/>
</level2>
</level1>
<level1>
<interestingstuff number = "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="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xst="http://www.w3.org/2005/02/xpath-datatypes"
exclude-result-prefixes="xs fn xdt">

<xsl:output
method="xml"
version="1.0"
encoding="ISO-8859-1"
indent="yes"/>

<xsl:template match="/">

<xsl:variable name="myNumbers">
<xsl:for-each select="//interestingstuff">
<xsl:sort select="./@number" data-type="number"
order="ascending" />
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>

<xsl:for-each select='root'>
<MyElement>
<xsl:attribute name="TimeStamp">
<xsl:value-of select="$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
XML Spy 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.

--
jon martin solaas
jon_martin_solaas ¤ y-a-h-o-o...n-o
Sep 29 '05 #1
2 1819


Jon Martin Solaas wrote:

I have a general document somewhat like this:
--------------------------------------
<root>
<level1>
<level2>
<interestingstuff number="2"/>
<interestingstuff number="3"/>
<interestingstuff number="1"/>
</level2>
<level2>
<interestingstuff number="10"/>
</level2>
</level1>
<level1>
<interestingstuff number = "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.


With XSLT 1.0 you could simply do the following in two top level variables

<xsl:variable name="numbers"
select="//interestingstuff/@number" />

<xsl:variable name="minNumber"
select="$numbers[not (. &gt; $numbers)]" />

and then use $minMumber where needed.

I suspect XSLT 2.0/XPath 2.0 even give you functions to find the minimum.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 29 '05 #2
Under XSLT 2.0 the value of a variable declared like this:

<xsl:variable name="myNumbers">
<xsl:for-each select="//interestingstuff">
<xsl:sort select="./@number" data-type="number"
order="ascending" />
<xsl:copy-of select="."/>
</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="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="2"/>
<interestingstuff number="3"/>
<interestingstuff number="1"/>
</level2>
<level2>
<interestingstuff number="10"/>
</level2>
</level1>
<level1>
<interestingstuff number = "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="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xst="http://www.w3.org/2005/02/xpath-datatypes"
exclude-result-prefixes="xs fn xdt">

<xsl:output
method="xml"
version="1.0"
encoding="ISO-8859-1"
indent="yes"/>

<xsl:template match="/">

<xsl:variable name="myNumbers">
<xsl:for-each select="//interestingstuff">
<xsl:sort select="./@number" data-type="number"
order="ascending" />
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>

<xsl:for-each select='root'>
<MyElement>
<xsl:attribute name="TimeStamp">
<xsl:value-of select="$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
XML Spy 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.

--
jon martin solaas
jon_martin_solaas ¤ y-a-h-o-o...n-o


Oct 3 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
4
by: David Blickstein | last post by:
I have an application where I have a shared "method" of selecting a nodeset. The method currently is duplicated everywhere it is needed but I'd like that code to be shared. The method also...
2
by: Michael Hamm | last post by:
I have the following XML file (simplified from the actual): <r> <o><n>1</n><si>s</si><v1>1</v1><v2>2</v2><v3>3</v3></o> <o><n>2</n><si>i</si><v1>4</v1><v2>5</v2><v3>6</v3></o>...
0
by: DAnne | last post by:
Hi, I'm very new to xslt and this is my first time posting to a Forum so please forgive me if I transgress any protocols. I have to do a tally report. This report is divided up into sections....
4
by: Michal | last post by:
Hi everyone, Microsoft states that the System.XML 1.0 included in framework 1.1 supports the W3C XSLT Specification. However, I am having a problem using the document() fuction specified in...
2
by: 张韡武 | last post by:
We have preffered language set as variable in xslt: <xsl:variable name="preferred_language"> zh </xsl:variable> Data: <name xml:lang="de">Raw Materials (Mining incl.)</name> <name...
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.