Reputation: 1521
I am having the following XML:
<?xml version="1.0"?>
<abc:Element1 xmlns:abc="http://..../resources/abc/v2/"
...>
<abc:Element2>
<abc:Element3s>
<abc:Element4 name="name1"
resourceRef="name2"/>
</abc:Element3s>
</abc:Element2>
<abc:Resources>
<abc:Resource xsi:type="abc:Something"
name="name2"/>
</abc:Resources>
</abc:Element1>
... and this XSLT stylesheet:
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="http://.../resources/abc/v2/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="/">
<checker name="something">
<xsl:for-each select="abc:Element1/abc:Element2/abc:Element3s/abc:Element4">
<xsl:variable name="resource" select="@resourceRef"/>
<xsl:variable name="xsiType"><xsl:value-of select="//abc:Resource[@name=$resource]/@xsi:type"/></xsl:variable>
<xsl:choose>
<xsl:when test="$xsiType='abc:Something'">
...
</xsl:when>
<xsl:otherwise>
...
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</checker>
</xsl:template>
</xsl:stylesheet>
I am using XALAN 2.7.1 with org.apache.xalan.xsltc.trax.TransformerFactoryImpl (also tried with org.apache.xalan.processor.TransformerFactoryImpl -> same result) to transform the XML.
I expect the following line to store abc:Something in variable xsiType.
<xsl:variable name="xsiType"><xsl:value-of select="//abc:Resource[@name=$resource]/@xsi:type"/></xsl:variable>
but unfortunatley only Something (without namespace as prefix) is stored in xsiType. I verified this because
<xsl:when test="$xsiType='abc:Something'">
is not true.
I also transformed the XMl using xsltproc and the resulting XML looks as expected. Therefore, I expect the input XML/XSLT stylesheet to be correct. I assume something is wrong with Xalan and its configuration.
Can anyone help?
Upvotes: 1
Views: 2391
Reputation: 163625
It looks to me like a problem specific to Xalan. It could however be a problem with the underlying XML parser: the default parser in th Sun JDK has some weird bugs including some that corrupt attribute values. Always use the Apache versions of Xalan and Xerces rather than the versions that come with the JDK. And of course, if you're using Xalan then it's almost zero cost to switch to Saxon, which gives you all the benefits of XSLT 2.0.
Upvotes: 1
Reputation: 15314
Your sample data is not well-formed, so it's difficult to tell. This is likely to be a namespace issues. Here's a sanitized version of your input and stylesheet that extracts the data you want:
<?xml version="1.0"?>
<abc:Element1
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:abc="http://resources/abc/v2">
<abc:Element2>
<abc:Element3s>
<abc:Element4 name="name1" resourceRef="name2"/>
</abc:Element3s>
</abc:Element2>
<abc:Resources>
<abc:Resource xsi:type="abc:Something" name="name2"/>
</abc:Resources>
</abc:Element1>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:abc="http://resources/abc/v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="abc xsi"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="resource" select="'name2'"/>
<xsl:variable name="type"
select="//abc:Resource[@name=$resource]/@xsi:type"/>
<checker name="{ $type }"/>
</xsl:template>
</xsl:stylesheet>
This will produce:
<?xml version="1.0"?>
<checker name="abc:Something"/>
Upvotes: 1