Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort data from multiple xml documents

Status
Not open for further replies.

rotzooischaap

Technical User
Jun 16, 2009
8
NL
I'm trying to sort number data from multiple xml documents. I've tried a couple of possibilities like putting sort in a for-each loop and in a apply-templates. Every time the numbers come up, but the data won't sort.
Example:
<xsl:template match="talents">
<xsl:for-each select="talent">
<xsl:variable name="talent" select="concat(.,'.xml')"/>
<xsl:apply-templates select="document($talent)//cv/studieverleden/master/cijfer">
<xsl:sort data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:for-each>
<br/>
<xsl:apply-templates select="talent">
<xsl:sort order="ascending"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="cijfer">
<xsl:value-of select="."/><br/>
</xsl:template>

<xsl:template match="talent">
<xsl:value-of select="."/><br/>
</xsl:template>

In this, the talent data is from 1 xml document ans id sorted, but the "cijfer" data is from multiple documents and there's no way to get it sorted.

Another example:
<xsl:template match="talents">
<table border='1'>
<thead><tr><td colspan = '3' align='center' bgcolor='orange'>
Master Cijfers</td></tr>
</thead>
<xsl:for-each select="talent">
<xsl:variable name="talent" select="concat(.,'.xml')"/>
<tr><td>
<xsl:value-of select="document($talent)//cv/studieverleden/master/cijfer" />
</td><td>
<xsl:value-of select="." />
</td></tr>
</xsl:for-each>
</table>
</xsl:template>

Hopefully one of you knows an answer to this problem.
 
You need to tell it what to sort.

><xsl:sort data-type="number" order="descending"/>
[tt]<xsl:sort [red]select="."[/red] data-type="number" order="descending"/>[/tt]
meaning sorting cijfer in that context.

><xsl:sort order="ascending"/>
[tt]<xsl:sort [red]select="."[/red] order="ascending"/>[/tt]
meaning sorting talent in that context.
 
Already tried that. Stupid that I didn't post that example as well, here it is:

<xsl:template match="talents">
<xsl:for-each select="talent">
<xsl:variable name="talent" select="concat(.,'.xml')"/>
<xsl:apply-templates select="document($talent)//cv/studieverleden/master/cijfer">
<xsl:sort data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:for-each>
<br/>
<xsl:apply-templates select="talent">
<xsl:sort order="descending"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="cijfer">
<xsl:value-of select="."/><br/>
</xsl:template>

<xsl:template match="talent">
<xsl:value-of select="."/><br/>
</xsl:template>

As you can see I used that data-type in sort, but the output was still the same. If you need the xml file just tell me and I will post it.
 
I am not sure I highlight (coloured red) the corrections enough for you to notice. Should I do it again?

[tt]<xsl:sort [highlight]select="."[/highlight] data-type="number" order="descending"/>[/tt]

[tt]<xsl:sort [highlight]select="."[/highlight] order="ascending"/>[/tt]

The "." is not sacrosanct. It only means the context I mentioned. It can be any of the child of it holding the text/number you want the sorting is against. I have no way to know without seeing the xml (but I don't want to see it unless you still have difficulty.)
 
[0] Let me get the idea through with an example.
[1] Let data.xml be such.
[tt]
<root>
<file>data_01</file>
<file>data_02</file>
</root>
[/tt]
[2] Let the external data files (data_01.xml, data_02.xml) be these.
[2.1] data_01.xml
[tt]
<rootext>
<number>
<int>1</int>
<dec>4</dec>
</number>
<number>
<int>3</int>
<dec>2</dec>
</number>
<number>
<int>2</int>
<dec>3</dec>
</number>
<number>
<int>4</int>
<dec>1</dec>
</number>
</rootext>
[/tt]
[2.2] data_02.xml
[tt]
<rootext>
<number>
<int>11</int>
<dec>11</dec>
</number>
<number>
<int>13</int>
<dec>13</dec>
</number>
<number>
<int>12</int>
<dec>12</dec>
</number>
<number>
<int>14</int>
<dec>14</dec>
</number>
</rootext>
[/tt]
[3] Construct the xsl like yours.
[tt]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]">
<xsl:eek:utput method="xml" indent="yes" encoding="utf-8" />
<xsl:template match="/">
<root>
<xsl:apply-templates select="root/file" />
</root>
</xsl:template>
<xsl:template match="file">
<xsl:variable name="lookup" select="concat(.,'.xml')" />
<xsl:apply-templates select="document($lookup)/rootext">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="rootext">
<xsl:apply-templates select="number">
<!--
<xsl:sort select="int" data-type="number" order="ascending" />
-->
<xsl:sort select="dec" data-type="number" order="ascending" />
<!--
<xsl:sort select="concat(int,'.',dec)" data-type="number" order="ascending" />
-->
</xsl:apply-templates>
</xsl:template>
<xsl:template match="number">
<x><xsl:value-of select="concat(int,'.',dec)" /></x>
</xsl:template>
</xsl:stylesheet>
[/tt]
[4] Switch around the options commented out for the xsl:sort element to inspect the effect.
[4.1] I understand yours, select="." being the default and can be spared. The above demo construction should amply show you if there is anything that can go wrong in your documents.
 
I don't have to show you the xml, because for this problem that's almost exactly how my xml looks like. Only difference is that
<root>
<file>data_01</file>
<file>data_02</file>
</root>
looks like
<talents>
<talent>data_01</talent>
<talent>data_02</talent>
</talents>
and that I call talent in a for-each loop in template talents.
Sorry that I didn't say that I noticed the select="." , but I tried both optionsalready, but I'll put it in the sort function again.

Í'm at work now, but I'll try those option you gave me as soon as possible.
 
I finally was aible to test you options, but all three option gave back three dots in a row.

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:eek:utput method="xml" indent="yes" encoding="utf-8" />
<xsl:template match="/">

<html>
<head>
<title>Cijfers</title>
</head>
<body>
<xsl:apply-templates select="talents/talent" />
</body>
</html>

</xsl:template>
<xsl:template match="talent">
<xsl:variable name="talent" select="concat(.,'.xml')" />
<xsl:apply-templates select="document($talent)//cv/studieverleden/master">
</xsl:apply-templates>
</xsl:template>

<xsl:template match="master">
<xsl:apply-templates select="cijfer">
<!--
<xsl:sort select="int" data-type="number" order="ascending" />
-->
<!--
<xsl:sort select="dec" data-type="number" order="ascending" />
-->
<!--
<xsl:sort select="concat(int,'.',dec)" data-type="number" order="ascending" />
-->
</xsl:apply-templates>
</xsl:template>

<xsl:template match="cijfer">
<x><xsl:value-of select="concat(int,'.',dec)" /></x>
</xsl:template>
</xsl:stylesheet>

I don't know if you can make sense out of this output?
 
That is not how you test/study the demo!!! Use exactly what I posted including the data and xsl files. Data and the original xml file are in the same directory.

If you really don't understand and need help, show one of your data file see if I can give a quick look!
 
I didn't really understand what you ment, but I copied your examples and constructed the data_01/02 and data.xml and the xsl and tested all three options. The second and third one worked so I tried them on my one files, but didn't get it to work. Perhaps you can see whats wrong.
You can get the files at:
 
I just figured out that your options don't work for me. In all xml files there's only one number and all these together need to be sorted. With your option numbers can only be sorted per xml file. If you change your example numbers to:
(note: I only changed two int's:
in data_01 I changed 2 > 5
in data_02 I changed 12 > 2)

[2.1] data_01.xml

<rootext>
<number>
<int>1</int>
<dec>4</dec>
</number>
<number>
<int>3</int>
<dec>2</dec>
</number>
<number>
<int>5</int>
<dec>3</dec>
</number>
<number>
<int>4</int>
<dec>1</dec>
</number>
</rootext>

[2.2] data_02.xml

<rootext>
<number>
<int>11</int>
<dec>11</dec>
</number>
<number>
<int>13</int>
<dec>13</dec>
</number>
<number>
<int>2</int>
<dec>12</dec>
</number>
<number>
<int>14</int>
<dec>14</dec>
</number>
</rootext>

Then the output is:
1 3 4 5 2 11 13 14
instead of:
1 2 3 4 5 11 13 14
 
Maybe you tell which one is the data file? (I really don't want to peep unnecessarily into your works.)
 
The ones I'm concerned about are
MC.xml
kandidaat 1.xml
kandidaat 2.xml
kandidaat 3.xml
MCcijfers.xsl

MC.xml is the data.xml in your example
 
The sorting is against ..../cijfer within each document of $talent, it won't do across. The demo also shows the same.
 
Is there any way to sort the data from all files together? Perhaps by using XQuery?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top