xml - Incorrect behaviour of xsl:for-each in Java -
i'm seeing inconsistent behaviour when applying xslt java program compared testing xml spy. specifically, when using java, xsl:for-each
directive not seem iterate on entire list. not case when testing xml spy.
my xml document:
<metadata xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <maps> <geographiccoverages> <coverage key="on">ontario</coverage> <coverage key="mb">manitoba</coverage> <coverage key="sk">saskatchewan</coverage> <coverage key="ab">alberta</coverage> <coverage key="bc">british columbia</coverage> <coverage key="yt">yukon</coverage> <coverage key="nu">nunavut</coverage> </geographiccoverages> </maps> <entry> <string>geo_coverage</string> <list> <string>bc</string> <string>on</string> </list> </entry> ... </metadata>
my xslt:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:output method="text" version="1.0" indent="yes"/> <xsl:key name="geocoveragekey" match="metadata/maps/geographiccoverages/coverage" use="@key" /> <xsl:template match="/metadata"> <xsl:for-each select="entry[string='geo_coverage']/list"> <xsl:value-of select="key('geocoveragekey', string)" separator=", " /> </xsl:for-each> </xsl:template> </xsl:stylesheet>
when test above xslt xmlspy, expected output:
ontario, british columbia
my java test program:
public static void main(string[] args) throws transformerexception { string xsltpath = "test_migration.xslt"; string xmlpath = "test-migration.xml"; transformerfactory tfactory = transformerfactory.newinstance(); transformer transformer = tfactory.newtransformer(new streamsource(xsltpath)); streamresult result = new streamresult( new stringwriter() ); transformer.transform(new streamsource(xmlpath), result ); system.out.println( result.getwriter().tostring() ); }
however, when run above java test program, output consists of 1 value only; ontario. i'm @ loss why i'm getting difference.
the default processor in jdk xalan. xalan xslt 1.0 processor. when xslt 1.0 processor given stylesheet specifies version="2.0", required (under rules xslt 1.0 processors) ignore attributes not defined in xslt 1.0. "separator" attribute falls category. in xslt 1.0, xsl:value-of outputs first node in node-set , ignores rest.
the answer install saxon, provides xslt 2.0 processing in java.
Comments
Post a Comment