0

This is the XML i have:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>
<catalog>
<cd>
    <title>Stop</title>
    <artist>Sam Brown</artist>
    <country>UK</country>
    <company>A and M</company>
    <price>8.90</price>
    <year>1988</year>
  </cd>
  <cd>
    <title>Bridge of Spies</title>
    <artist>T`Pau</artist>
    <country>UK</country>
    <company>Siren</company>
    <price>7.90</price>
    <year>1987</year>
  </cd>
</catalog>

Following is the XSLT where I am trying to get the data from both root nodes :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title" /></td>
        <td><xsl:value-of select="artist" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

It returns only 2 records. I need to get 4 records. What should I write in the select statement. I tried backslash, asterisk. Didn't work. Please help!

Or how do I enclose the two root nodes into a single root node in xslt?

Actual Output:

My CD Collection
Title                   Artist
------------------------------------
Empire Burlesque        Bob Dylan
Hide your heart         Bonnie Tyler

Desired Output:

My CD Collection
Title                   Artist
------------------------------------
Empire Burlesque        Bob Dylan
Hide your heart         Bonnie Tyler
Stop                    Sam Brown
Bridge of Spies         T`Pau

You can try this link to test. Please don't forget to copy paste the xml mentioned above: https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex3

  • By W3C 1.0 specifications for [well-formed documents](https://www.w3.org/TR/xml/#sec-well-formed), an XML document cannot have two roots. Use compliant DOM libraries or fix the use of their methods for its generation. Do not treat XML as a text file which usually is the reason for malformed markup. – Parfait Sep 01 '20 at 14:51
  • 1
    That input should give an error from the underlying XML parser as soon as the second `catalog` element is parsed. – Martin Honnen Sep 01 '20 at 16:09
  • If the XML cannot have two root nodes, then we can enclose these two root nodes into one as catalogs node. How do we achieve this in XSLT. Please help. – user14203304 Sep 02 '20 at 05:45
  • please leave the current output and desired output in your question to help others get your question better! @user14203304 – mohammad hassan bigdeli shamlo Sep 02 '20 at 10:42
  • @mohammadhassanbigdelishamlo Edited the question for better understanding – user14203304 Sep 02 '20 at 12:32

0 Answers0