csteinhilber
Programmer
I have the following XML generated by a legacy system.
I can think of many ways to reformat the XML to make it more useful, but unfortunately that's not possible. Yet I still need to be able to apply XSLT to transform it into usable output (in this case, just HTML).
Anyway... the challenge is to apply unique templates depending on the type of node of each retail item (book, cassette, etc).
But the problem is that each of the items needs to output in the order that they are listed in the XML. So I can't simply do a
for instance (I don't think), because that would innevitably group all the books together.
The solution I thought would work would be to use a choose:when block... but I can't figure out how to compare the node type in the test expression.
I tried something like:
for the above XML, the desired output (at this testing stage at least) should be:
But I only get "Unrecognized item type" (and only one instance at that... which is equally confusing).
Anyone know how to compare the actual node type/name in a test expression? Or have any other insight as to how this could be accomplished?
Thanks!
-Carl
Code:
<retail_items>
<book>
<title>...</title>
<author>...</author>
<publish_date>...</publish_date>
</book>
<book>
<title>...</title>
<author>...</author>
<publish_date>...</publish_date>
</book>
<cassette>
<title>...</title>
<artist>...</artist>
<tracks>
<track>...</track>
</tracks>
</cassette>
<periodical>
<title>...</title>
<publisher>...</publisher>
<frequency>...</frequency>
</periodical>
<book>
<title>...</title>
<author>...</author>
<publish_date>...</publish_date>
</book>
</retail_items>
I can think of many ways to reformat the XML to make it more useful, but unfortunately that's not possible. Yet I still need to be able to apply XSLT to transform it into usable output (in this case, just HTML).
Anyway... the challenge is to apply unique templates depending on the type of node of each retail item (book, cassette, etc).
But the problem is that each of the items needs to output in the order that they are listed in the XML. So I can't simply do a
Code:
<xsl:template match="book">
The solution I thought would work would be to use a choose:when block... but I can't figure out how to compare the node type in the test expression.
I tried something like:
Code:
<xsl:stylesheet version="1.0"
xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:template match="/">
<xsl:apply-templates select="retail_items" />
</xsl:template>
<xsl:template match="retail_items">
<xsl:choose>
<xsl:when test="@XmlName = 'book'">
<!-- eventually this would stub out to a template specifically for formatting a book, etc -->
<p>It's a book!</p>
</xsl:when>
<xsl:when test="@XmlName = 'cassette'">
<p>It's a cassette!</p>
</xsl:when>
<xsl:when test="@XmlName = 'periodical'">
<p>It's a periodical!</p>
</xsl:when>
<xsl:otherwise>
<p>Unrecognized item type</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
for the above XML, the desired output (at this testing stage at least) should be:
Code:
It's a book
It's a book
It's a cassette
It's a periodical
It's a book
But I only get "Unrecognized item type" (and only one instance at that... which is equally confusing).
Anyone know how to compare the actual node type/name in a test expression? Or have any other insight as to how this could be accomplished?
Thanks!
-Carl