0

I have the following XML code:

http://codeshare.io/MQyQu

and am using the following XSLT code:

http://codeshare.io/LWl6g

in coordination with the W3 Schools "Tryit Editor" tool for XSLT. I can't link it due to reputation.

My problem is that the XSLT does not parse all the elements (semester elements within a catalog element, and class elements within a semester element).

The W3 schools example of XSLT uses only one subelement, and I'm at a loss for what to do for two subelements.

My understanding is that the first

  <xsl:for-each select="catalog/semester">

will loop semester elements in the catalog elements.

Within the loop, I have a HTML table where I want to output the information of the class element, that is a subelement of semester. I think nesting this code in the original for-each loop should output the text for each element for each class in the table:

<xsl:for-each select="class">

    <td><xsl:value-of select="dept"/></td>
    <td><xsl:value-of select="number"/></td>
    <td><xsl:value-of select="title"/></td>
</xsl:for-each>

In the HTML table, only the text, "CIT", of the first subelement, dept, of the element class is displayed.

I deduct that the for-each loop isn't parsing. If that is the reason, why?

If my deduction on why the XSLT code isn't reading the XML code how I want it is incorrect, what is the correct reason and how can I fix it?

1 Answers1

2

There are several problems here...

  1. Your input XML isn't well formed - there are tags that begin <number> but aren't closed (it seems like the tag that ends with </dept> is meant to close it).
  2. Your input XML contains unescaped ampersands. Replace & with &amp;
  3. Your Stylesheet is invalid because it's missing a </table> tag. You need to add it in.
  4. Your inner for-each loop should probably have <tr> and </tr> tags around the <td>...</td> nodes.

Here's an XSLTransform with all of these problems addressed (and I added an output instruction so that XSLTransform can show it as HTML in the window): http://xsltransform.net/94rmq66

So the bottom line is that you're right, the XSLT parser can't read the XML because it's malformed XML. The fact that it's doing anything at all with your input is pretty bad, and another reason not to use the W3Schools XSLT tools.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
  • 1
    Good (+1) [but replace ***not valid*** with ***not well-formed***](http://stackoverflow.com/a/25830482/290085). – kjhughes Nov 16 '15 at 20:00