Reputation: 81
I'm trying to use a single XSLT file to transform 2 (or more) different types of XML files.
I was hoping there would be some way to perform a check for the type of XML file by checking its "id" value and then run the corresponding transformation.
For example,
XML file 1:
<?xml version='1.0' ?>
<file1 id="123">
<key>
<uuid>123456</uuid>
</key>
</file1>
XML file 2:
<?xml version='1.0' ?>
<file2 id="456">
<house>
<doors>1</doors>
<windows>4</windows>
</house>
</file2>
XSLT file:
<?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>
<xsl:choose>
<xsl:when test="@id = 123">
// do something
</xsl:when>
<xsl:when test="@id = 456">
// do something else
</xsl:when>
<xsl:otherwise>
// exception message
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The XML files will all have the same "id" in their root but I can't figure out how to query the root for the "id" value?
Upvotes: 0
Views: 70
Reputation: 6677
To fetch an attribute on the root element (regardless what the root element is), use /*/@id
.
<xsl:choose>
<xsl:when test="/*/@id = 123">
// do something
</xsl:when>
<xsl:when test="/*/@id = 456">
// do something else
</xsl:when>
<xsl:otherwise>
// exception message
</xsl:otherwise>
</xsl:choose>
Upvotes: 1