473,591 Members | 2,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT variable problem

Hi,

I have a general document somewhat like this:
--------------------------------------
<root>
<level1>
<level2>
<interestingstu ff number="2"/>
<interestingstu ff number="3"/>
<interestingstu ff number="1"/>
</level2>
<level2>
<interestingstu ff number="10"/>
</level2>
</level1>
<level1>
<interestingstu ff number = "11"/>
</level1>
</root>
--------------------------------------

Now, I want to use the lowest number value from the interestingstuf f
nodes' number attribute in various places in an xslt transformation.

My xslt looks like this:
--------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:styleshe et
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="//interestingstuf f">
<xsl:sort select="./@number" data-type="number"
order="ascendin g" />
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>

<xsl:for-each select='root'>
<MyElement>
<xsl:attribut e name="TimeStamp ">
<xsl:value-of select="$myNumb ers[1]/@number"/>
</xsl:attribute>
</MyElement>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
--------------------------------------

The idea is to store a sorted list of interestingstuf f-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_sola as ¤ y-a-h-o-o...n-o
Sep 29 '05 #1
2 1835


Jon Martin Solaas wrote:

I have a general document somewhat like this:
--------------------------------------
<root>
<level1>
<level2>
<interestingstu ff number="2"/>
<interestingstu ff number="3"/>
<interestingstu ff number="1"/>
</level2>
<level2>
<interestingstu ff number="10"/>
</level2>
</level1>
<level1>
<interestingstu ff number = "11"/>
</level1>
</root>
--------------------------------------

Now, I want to use the lowest number value from the interestingstuf f
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="//interestingstuf f/@number" />

<xsl:variable name="minNumber "
select="$number s[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="//interestingstuf f">
<xsl:sort select="./@number" data-type="number"
order="ascendin g" />
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
is a temporary tree: a document node owning zero or more
interestingStuf f 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>
<interestingstu ff number="2"/>
<interestingstu ff number="3"/>
<interestingstu ff number="1"/>
</level2>
<level2>
<interestingstu ff number="10"/>
</level2>
</level1>
<level1>
<interestingstu ff number = "11"/>
</level1>
</root>
--------------------------------------

Now, I want to use the lowest number value from the interestingstuf f
nodes' number attribute in various places in an xslt transformation.

My xslt looks like this:
--------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:styleshe et
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="//interestingstuf f">
<xsl:sort select="./@number" data-type="number"
order="ascendin g" />
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>

<xsl:for-each select='root'>
<MyElement>
<xsl:attribut e name="TimeStamp ">
<xsl:value-of select="$myNumb ers[1]/@number"/>
</xsl:attribute>
</MyElement>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
--------------------------------------

The idea is to store a sorted list of interestingstuf f-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_sola as ¤ 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
2693
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 extension responsible for the xslt functions. The problem i have is that the obtained transformation is not the waited one. I try to proccess the same XML file with XSL file with a program called XMLspy and i obtained the desire and waited...
6
2902
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 this, and came across a problem that I haven't been able to solve elegantly. The problem is to find "linker" vertexes that a pair of verteces from a pre-defined set. For example, if the graph verteces represent cities and edges represent flights...
4
21198
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 native English speaker, it's quite hard to make the problem clear. Please see the following example.
4
3236
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 name="Banner_error_2"> errormessage 2 for banner </xsl:variable>
4
1735
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 includes a conditional test (FWIW, the test is whether a stylesheet parameter has a certain string value.) <xsl:choose <xsl:when test='$Generate='Y'"> select node set one way <xsl:otherwise> select node set another way
2
1856
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> <o><n>3</n><si>s</si><v1>7</v1><v2>8</v2><v3>9</v3></o> <o><n>5</n><si>i</si><v1>10</v1><v2>11</v2><v3>12</v3></o> <o><n>6</n><si>i</si><v1>13</v1><v2>14</v2><v3>15</v3></o> </r>
0
3289
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. Each section has a list of questions. Each question has responses. I need to display a list of responses to the questions (i.e. set:distinct), once and only once, each section. My second problem is that these questions can also have corrective...
4
1609
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 http://www.w3.org/TR/xslt (chapter 12.1). When using "<xsl:variable name="test" select="document('Resources.xml')/root/test"/>" the variable test is always empty. However when debugging this select in the Stylus studio, the variable is correctly...
2
2094
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 xml:lang="zh">原æ料(包括采矿业) </name>
3
9573
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 difference between two source documents and output node-sets which are "different" (changed or new) to new XML files using xsl:result-document To describe the problem I have provided some example data below along with my a portion of my current...
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7870
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5732
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.