0

I have two files.

Content of f1 is:

   <applianceVersion>
    <id>main</id>
    <version>0301</version>
    <components>
        <component>
            <id>A</id>
            <version>a1</version>
        </component>
        <component>
            <id>B</id>
            <version>b1</version>
        </component>
        <component>
            <id>C</id>
            <version>c1</version>
        </component>
    </components>
</applianceVersion>    

Content of f2 is:

<applianceVersion>
    <id>main</id>
    <version>0301</version>
    <components>
        <component>
            <id>A</id>
            <version>a2</version>
        </component>
        <component>
            <id>B</id>
            <version>b2</version>
        </component>
        <component>
            <id>C</id>
            <version>c3</version>
        </component>
    </components>
</applianceVersion>

The two files will always have the same components. My goal is to show both content side by side like the following:

main    0301    0302
A       a1      a2
B       b1      b2
C       c1      c2

I have the following xsl applied to f1:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <table border="1">
    <tr bgcolor="#87E0F8">
      <th style="text-align:left"><xsl:value-of select="applianceVersion/id"/></th>
      <th style="text-align:left"><xsl:value-of select="applianceVersion/version"/></th>
      <th style="text-align:left"><xsl:value-of select="document('f2.xml')/applianceVersion/version"/></th>
    </tr>
    <tr>
    </tr>
    <xsl:for-each select="applianceVersion/components/component">
    <tr>
      <td><xsl:value-of select="id"/></td>
      <td><xsl:value-of select="version"/></td>
      <xsl:for-each select="document('f2.xml')/applianceVersion/components/component[id=<xsl:value-of select="id"/>]">
      <td><xsl:value-of select="version"/></td>
      </xsl:for-each>
    </tr>
    </xsl:for-each>
  </table>
</xsl:template>
</xsl:stylesheet>

I am a newbie to xslt.. And the above code is erroring out.

So the questions are:

  1. How do I reference "id" within the for-each filter to take up different value?
  2. Is there a better way to accomplish the same?
  • Please post the **exact** result you expect to get. If - as it seems from your attempt - the result needs to be a HTML table, post the HTML code. -- Also your results shows the value `0302` - but there is no such value in either one of the input documents. – michael.hor257k Mar 03 '21 at 07:43

1 Answers1

1

I am guessing (!) you want to do something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="f2" select="document('f2.xml')/applianceVersion" />

<xsl:template match="/applianceVersion">
    <table border="1">
        <!-- header -->
        <tr>
            <th>
                <xsl:value-of select="id"/>
            </th>
            <th>
                <xsl:value-of select="version"/>
            </th>
            <th>
                <xsl:value-of select="$f2/version"/>
            </th>
        </tr>
        <!-- body -->
        <xsl:for-each select="components/component">
            <tr>
                <td>
                    <xsl:value-of select="id"/>
                </td>
                <td>
                    <xsl:value-of select="version"/>
                </td>
                <td>
                    <xsl:value-of select="$f2/components/component[id=current()/id]/version"/>
                </td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>

Apllied to your input documents*, this will produce:

Result

<?xml version="1.0" encoding="UTF-8"?>
<table border="1">
  <tr>
    <th>main</th>
    <th>0301</th>
    <th>0302</th>
  </tr>
  <tr>
    <td>A</td>
    <td>a1</td>
    <td>a2</td>
  </tr>
  <tr>
    <td>B</td>
    <td>b1</td>
    <td>b2</td>
  </tr>
  <tr>
    <td>C</td>
    <td>c1</td>
    <td>c3</td>
  </tr>
</table>

which renders as:

enter image description here

A better way might be to use a key to retrieve the corresponding value from the other XML document. But the exact method depends on which version of XSLT your processor supports - see: https://stackoverflow.com/a/30188334/3016153.

Alternatively, if the two documents contain the same components, in the same order, you could do simply:

    <!-- body -->
    <xsl:for-each select="components/component">
        <xsl:variable name="i" select="position()" />
        <tr>
            <td>
                <xsl:value-of select="id"/>
            </td>
            <td>
                <xsl:value-of select="version"/>
            </td>
            <td>
                <xsl:value-of select="$f2/components/component[$i]/version"/>
            </td>
        </tr>
    </xsl:for-each>

--
(*) after changing <version>0301</version> in f2.xml to <version>0302</version>.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51