Author Topic: Calling all XML/XSL experts....  (Read 2122 times)

tiermat

  • According to Jane, I'm a Unisex SpaceAdmin
Calling all XML/XSL experts....
« on: 23 September, 2008, 10:20:28 am »
I have an XML file, and a XSL file to transform said XML into a nice table.  Trouble is that the XSL is picking up the right number of lines to put in the table, but not the data.  Where am I going wrong?

First the XSL

Quote
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<!-- / forward slash is used to denote a patern that matches the root node of the XML document -->
<xsl:template match ="/" >
    <html>
      <head>
        <title> RedHat RPMS </title>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
</xsl:template>

<xsl:template match="comps" >
    <table width="400" border="1" >
        <tr bgcolor = "#cccccc" >
            <td>Group</td>
            <td>Packages</td>
       </tr>
<xsl:for-each select="group" >
        <tr>
            <td> <xsl:value-of select="id" /> </td>
            <xsl:for-each select="packagelist/packagereq">
                <xsl:sort select= "." />
            <td> <xsl:value-of select="packagereq" />
            </td>
            </xsl:for-each>       
        </tr>
</xsl:for-each>
     </table>
</xsl:template >
</xsl:stylesheet >

And the XML, well a snippet of it

Quote
<?xml version='1.0' encoding='UTF-8'?>
<!-- <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd"> -->
<comps>
  <group>
    <id>ISO8859-2-support</id>
    <name>ISO8859-2 Support</name>
    <default>false</default>
    <uservisible>false</uservisible>
    <biarchonly>false</biarchonly>
    <packagelist>
      <packagereq type='mandatory'>fonts-ISO8859-2</packagereq>
      <packagereq requires='xorg-x11' type='optional'>fonts-ISO8859-2-100dpi</packagereq>
      <packagereq requires='xorg-x11' type='optional'>fonts-ISO8859-2-75dpi</packagereq>
    </packagelist>
  </group>
  <group>
    <id>ISO8859-9-support</id>
    <name>ISO8859-9 Support</name>
    <default>false</default>
    <uservisible>false</uservisible>
    <biarchonly>false</biarchonly>
    <packagelist>
      <packagereq requires='xorg-x11' type='optional'>fonts-ISO8859-2-75dpi</packagereq>
      <packagereq requires='xorg-x11' type='optional'>fonts-xorg-ISO8859-9-100dpi</packagereq>
      <packagereq requires='xorg-x11' type='optional'>fonts-xorg-ISO8859-9-75dpi</packagereq>
    </packagelist>
  </group>
</comps>
I feel like Captain Kirk, on a brand new planet every day, a little like King Kong on top of the Empire State

Re: Calling all XML/XSL experts....
« Reply #1 on: 23 September, 2008, 05:43:41 pm »
I have not run this or anything - just had a quick look over it - but this *might* do it.

Code: [Select]
<xsl:for-each select="group" >
        <tr>
            <td> <xsl:value-of select="id" /> </td>
            <xsl:for-each select="packagelist/packagereq">
                <xsl:sort select= "." />
[b]            <td> <xsl:value-of select="." />[/b]
            </td>
            </xsl:for-each>      
        </tr>
</xsl:for-each>

Changed line bolded. [edit] Or not, but you can see where I tried to bold it? It's just changing packagereq to a .

tiermat

  • According to Jane, I'm a Unisex SpaceAdmin
Re: Calling all XML/XSL experts....
« Reply #2 on: 24 September, 2008, 08:57:07 am »
Marna! You rock!

that was the bit of my XSL training I had forgotten, i.e. when you select part of the tree, that then becomes the root....

Happy (relatively) now :)

thanks
I feel like Captain Kirk, on a brand new planet every day, a little like King Kong on top of the Empire State

tiermat

  • According to Jane, I'm a Unisex SpaceAdmin
Re: Calling all XML/XSL experts....
« Reply #3 on: 24 September, 2008, 10:30:07 am »
Now the next bit.

The above code works, but not quite how I would like it.

It produces a table, roughly thus

Col1     Col2          Col3          Col4
Group   Package1 Package2  Package3 etc etc etc

What I actually want is:

Col1         Col2
Group       Package1
Group       Package2
Group       Package3
Group1     Package1
etc
etc
etc

This bit is eluding me right now
I feel like Captain Kirk, on a brand new planet every day, a little like King Kong on top of the Empire State

Re: Calling all XML/XSL experts....
« Reply #4 on: 24 September, 2008, 10:38:30 am »
If you always have three elements, you could use position() to place a </td> after 2 and a <td> before 3.

tiermat

  • According to Jane, I'm a Unisex SpaceAdmin
Re: Calling all XML/XSL experts....
« Reply #5 on: 24 September, 2008, 10:40:44 am »
Unfortunately not, the packages vary from 3 in number, to >100....

I can feel my sanity slipping away as we speak :(

Oddly enough at one point yesterday I got it to produce the table with the right number of blank Packages cells besides each Group, but the packages were all then listed at the top of the output  ???
I feel like Captain Kirk, on a brand new planet every day, a little like King Kong on top of the Empire State

Re: Calling all XML/XSL experts....
« Reply #6 on: 24 September, 2008, 11:34:39 am »
If it's always to be two columns, how about making it odd v even numbers? That might be a bit long-winded.

bobajobrob

Re: Calling all XML/XSL experts....
« Reply #7 on: 24 September, 2008, 02:22:38 pm »
Code: [Select]
<xsl:for-each select="group" >
  <xsl:variable name="id" select="id" />
  <xsl:for-each select="packagelist/packagereq">
    <xsl:sort select= "." />
    <tr>
      <td> <xsl:value-of select="$id" /> </td>
      <td> <xsl:value-of select="." /> </td>
    </tr>
  </xsl:for-each>       
</xsl:for-each>

Or something ;)

This might do it too:

Code: [Select]
<xsl:for-each select="group" >
  <xsl:for-each select="packagelist/packagereq">
    <xsl:sort select= "." />
    <tr>
      <td> <xsl:value-of select="../../id" /> </td>
      <td> <xsl:value-of select="." /> </td>
    </tr>
  </xsl:for-each>       
</xsl:for-each>

tiermat

  • According to Jane, I'm a Unisex SpaceAdmin
Re: Calling all XML/XSL experts....
« Reply #8 on: 24 September, 2008, 02:39:41 pm »
Fools seldom differ bob, this is how I finally got what I needed:

Quote

...SNIP...

<xsl:for-each select="group" >
        <tr>
            <xsl:for-each select="packagelist/packagereq">
            <tr><td><xsl:value-of select="../../id" /></td>
            <td><xsl:value-of select="." /></td></tr>
            </xsl:for-each>
        </tr>
</xsl:for-each>

.../SNIP...

subtley different, but meaning the same thing....
I feel like Captain Kirk, on a brand new planet every day, a little like King Kong on top of the Empire State

Re: Calling all XML/XSL experts....
« Reply #9 on: 09 October, 2017, 11:10:11 am »
resurrecting an old thread . . .

I have no xsl training but for my sins have to poke about with it.

I'm a bit mystified by this:
$enumber-levels[$depth]

enumber-levels is defined by this:
<xsl:variable name="enumber-levels" select="(true(), true(), true(), false())" />

depth is a numerical count.

From a syntax guide, the syntax of e1[e2] "Square brackets enclose a predicate, which specifies an expression e2 that selects nodes from a larger set e1."

So I don't know how the variable 'depth' can be selecting from 'enumber-levels'.
<i>Marmite slave</i>

Ben T

Re: Calling all XML/XSL experts....
« Reply #10 on: 09 October, 2017, 01:46:27 pm »
try downloading altova mapforce, it's not free but you can get a free trial which might be enough for you to be able to extract what you need

Vince

  • Can't climb; won't climb
Re: Calling all XML/XSL experts....
« Reply #11 on: 09 October, 2017, 06:27:11 pm »
I'm probably under thinking this but...

Putting a group of values in parenthesis creates an array so
     <xsl:variable name="enumber-levels" select="(true(), true(), true(), false())" />
is an array with 4 values.

In common with many other programming languages putting an iterator in brackets after an array variable will give the particular entry in the array.

(Am I the only one who had to look up 'predicate' and am still none the wiser?)
216km from Marsh Gibbon

Re: Calling all XML/XSL experts....
« Reply #12 on: 09 October, 2017, 06:48:32 pm »
Ty - still not sure how a count relates to an array of boolean values. Bit of a moot point as I've found a neater solution for the problem - then found it won't work for my particular situation. Damn stuck.
<i>Marmite slave</i>

Vince

  • Can't climb; won't climb
Re: Calling all XML/XSL experts....
« Reply #13 on: 09 October, 2017, 07:27:26 pm »
If the count has three in it, array[count] will return the 4th entry from the array.
216km from Marsh Gibbon

Ben T

Re: Calling all XML/XSL experts....
« Reply #14 on: 09 October, 2017, 07:30:48 pm »
Has someone tried to return the last one but has failed to appreciate that it's zero based and you need to do $count - 1?
worth a try...

Re: Calling all XML/XSL experts....
« Reply #15 on: 10 October, 2017, 11:31:08 am »
If the count has three in it, array[count] will return the 4th entry from the array.
That sort of helps to make sense. Ty

Doesn't help with my problem, unfortunately.
<i>Marmite slave</i>