0

Thank you in advance for your help. I'm new with XSLT. I'm trying to create a table where I can show:

  • on the first column the values of the attribute "name" of
<schedaDescrittiva name="0001">.
  • on the second column the values of the attribute "name" of
<div type="Fascicolo" name="0001">
  • on the third column a comparison between the two values, highlighting with a color the cells where the two values don't match.

I tried different ways, but my efforts has been vain.

Here my XML (or, a part of it):

<xml>
    <schedeDescrittive>
        <schedaDescrittiva name="0001">
            <titolo>(La) Distribuzione della proprietà fondiaria in Dogliani</titolo>
            <descrizione>«Gazzetta di Dogliani» (Dogliani), a. 4, n. 199</descrizione>
        </schedaDescrittiva>
        <schedaDescrittiva name="0002">
            <titolo>Epistolario di studenti</titolo>
            <descrizione>«Crit. soc.», III, n. 13</descrizione>
        </schedaDescrittiva>
    </schedeDescrittive>
    <livelliGerarchici>
        <div type="Fondo" name="EL">
            <div type="Serie" name="SEZ.1">
                <div type="Sotto-serie" name="OIB">
                    <div type="Livello" name="1893">
                        <div type="Fascicolo" name="0001">
                            <div type="Copertina" ID="000008" link="./TIFF/EL_SEZ.1_OIB_000008_1893_2_01_0001.tif"/>
                        </div>
                        <div type="Fascicolo" name="0002">
                            <div type="Copertina" ID="000008" link="./TIFF/EL_SEZ.1_OIB_000008_1893_2_01_0002.tif"/>
                        </div>
    </livelliGerarchici>
</xml>

Here what I have been trying to do until now:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
    <html>
        <head>
        <style>table, th, td {border: 1px solid black;}</style>
        <tr>
            <th>Confronto Valori</th>
        </tr>
        </head>
        <body>
            <table>
                <tr>
                <th>Valori Scheda Bibliografica</th>
                <th>Valori Percorsi Cartelle</th>
                <th>Comparazione</th>
                </tr>
                <xsl:for-each select="xml/schedeDescrittive/schedaDescrittiva">
                <tr>
                    <td>
                        <xsl:value-of select="@name"></xsl:value-of>
                    </td>   
                </tr>
                </xsl:for-each>
                <xsl:for-each select="xml/livelliGerarchici/div/div/div/div/div">
                <tr>
                    <td>
                        <xsl:value-of select="@name"></xsl:value-of>
                    </td>   
                </tr>
                </xsl:for-each>
            </table>
      </body>
      </html>
</xsl:template>
</xsl:stylesheet>

My first problem is that the value of the attribute name:

<div type="Fascicolo" name="0001">

appears on the first column and not in the second column as I would like to be. The second problem is to compare the two values that are on the same row. For that I've no idea.

I would really appreciate any suggestion. Thank you, Ivan

Ivan
  • 1
  • 1

1 Answers1

0

I tried this and it looks like a good starting point. Note that I had to add a few closing tag </div> to make the document well-formed. The XPath expression count(./preceding-sibling::*)+1 compute the position of the element (comes from https://stackoverflow.com/a/227080/452614).

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
    <html>
        <head>
        <style>table, th, td {border: 1px solid black;}</style>
        <tr>
            <th>Confronto Valori</th>
        </tr>
        </head>
        <body>
            <table>
                <tr>
                <th>Valori Scheda Bibliografica</th>
                <th>Valori Percorsi Cartelle</th>
                <th>Comparazione</th>
                </tr>
                <xsl:for-each select="xml/schedeDescrittive/schedaDescrittiva">
                    <xsl:variable name="pos"><xsl:value-of select="count(./preceding-sibling::*)+1"/></xsl:variable>
                <tr>
                    <td>
                        <xsl:value-of select="@name"></xsl:value-of>
                    </td>
                    <td>
                        <xsl:value-of select="//livelliGerarchici//div[@type='Fascicolo'][position()=$pos]/@name"></xsl:value-of>
                    </td>
                </tr>
                </xsl:for-each>
            </table>
      </body>
      </html>
</xsl:template>
</xsl:stylesheet>

It produces the following document.

$ java -cp saxon.jar com.icl.saxon.StyleSheet file.xml file.xslt
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <style>table, th, td {border: 1px solid black;}</style><tr>
         <th>Confronto Valori</th>
      </tr>
   </head>
   <body>
      <table>
         <tr>
            <th>Valori Scheda Bibliografica</th>
            <th>Valori Percorsi Cartelle</th>
            <th>Comparazione</th>
         </tr>
         <tr>
            <td>0001</td>
            <td>0001</td>
         </tr>
         <tr>
            <td>0002</td>
            <td>0002</td>
         </tr>
      </table>
   </body>
</html>
Ludovic Kuty
  • 4,868
  • 3
  • 28
  • 42