This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes xsl getNext to sort output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xsl getNext to sort output" Watch "xsl getNext to sort output" New topic
Author

xsl getNext to sort output

michi michaela
Greenhorn

Joined: Apr 21, 2006
Posts: 6
hi guys - i'm really down because i cannot find / understand my xsl error and i have to finish my work very very soon

the error message (by using xalan to create from the xml file my html output) is the following:
xslt error: the expression does not evaluate to a node-set

and the code fragement is:


<xsl:template name="getNext">
<xsl aram name="currentKnoten" />
<xsl:for-each select="$currentKnoten"> //error is in this line
</xsl:for-each>
</xsl:template>



ok here is the xml code (simplified of course)


<DATA name="testdata">

<OTHERDATA></OTHERDATA>

<INSTANCE class="Start" name="Example Data">
<ATTRIBUTE name="stuff"></ATTRIBUTE>
</INSTANCE>
<INSTANCE class="OpenData" name="OpenData-44422">
<ATTRIBUTE name="Data">Last Name</ATTRIBUTE>
<ATTRIBUTE name="Comment"/>
</INSTANCE>
<INSTANCE class="OpenData" name="OpenData-44425">
<ATTRIBUTE name="Data">Adress</ATTRIBUTE>
<ATTRIBUTE name="Comment"/>
</INSTANCE>
INSTANCE class="ClosedData" name="ClosedData-44431">
<ATTRIBUTE name="Data">Gender</ATTRIBUTE>
<RECORD name="Choice of Responses">
<ROW>
<ATTRIBUTE name="Response" type="STRING">male</ATTRIBUTE>
</ROW>
<ROW>
<ATTRIBUTE name="Response" type="STRING">female</ATTRIBUTE>
</ROW>
</RECORD>
</INSTANCE>
<INSTANCE class="OpenData" name="OpenData-44459">
<ATTRIBUTE name="Data">First Name</ATTRIBUTE>
<ATTRIBUTE name="Comment"/>
</INSTANCE>

<CONNECTOR class="Sub">
<FROM instance="Example Data" class="Start"/>
<TO instance="OpenData-44459" class="OpenData"/>
</CONNECTOR>
<CONNECTOR class="Sub">
<FROM instance="OpenData-44459" class="OpenData"/>
<TO instance="Open Data-44422" class="OpenData"/>
</CONNECTOR>
... some more connectors .....
</DATA>





so what is the aim..... as u can see there is wrong ordner in the xml

last name
address
gender
first name

but the output should be:
first name
last name
adress
gender

there is one way to get the right order --> you can look on the subsequent part there is a FROM and TO and there u can see how the output should look like.... start is of course start...

now i go back to the origin problem... my main idea is the following xsl code... may u can see here the problem with the node site.. but i think u need the xml file or?

however this is my relevant xsl code:


<xsl:template match="DATA">
<xsl:element name="process">
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:apply-templates select="INSTANCE"/>
</xsl:element>
</xsl:template>

<xsl:template match="INSTANCE[@class='Data']">
<xsl:element name="node">
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:attribute name="class">
<xsl:value-of select="@class" />
</xsl:attribute>
<xsl:call-template name="copyAttributes" />
<xsl:variable name="nextNodes">
<xsl:value-of select="key('keyGetData', key('keyFrom', @name)/../TO/@instance)" />
</xsl:variable>
<xsl:call-template name="getNext">
<xsl:with-param name="currentNode" select="$nextNodes" />
</xsl:call-template>
</xsl:element>
</xsl:template>

<xsl:template name="getNext">
<xsl aram name="currentNode"/>
<xsl:for-each select="$currentNode">
<xsl:element name="node">
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="class"><xsl:value-of select="@class"/></xsl:attribute>
<xsl:variable name="nextNodes">
<xsl:value-of select="key('keyGetData', key('keyFrom', @name)/../TO/@instance)"/>
</xsl:variable>
<xsl:call-template name="copyAttributes"/>
<xsl:call-template name="getNext">
<xsl:with-param name="currentNode" select="$nextNodes"/>
</xsl:call-template>
</xsl:element>
</xsl:for-each>
</xsl:template>

<xsl:template name="copyAttributes">
<xsl:for-each select="descendant::*">
<xsl:copy>
<xsl:copy-of select="@*|node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:apply-templates/>
</xsl:template>


hopefully u understand what i mean....
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The error message is correct.You are putting a string in the variable, not a node-set. <xsl:value-of> takes its parameter -- which in this case was a node set -- and converts it to a string. So don't do that. Just assign the node-set to the variable.
michi michaela
Greenhorn

Joined: Apr 21, 2006
Posts: 6
thank you for your help... now it is working!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: xsl getNext to sort output