TechBreak TechBreak - 28 days ago 14
Java Question

XSL transformation with xml node (key) starts with number fails

I am facing an issue where if the XML response contains a node (key) element which starts with a number e.g. <3party />
and if I try to fetch this node from xml in XSL stylesheet than it fails with following exception. Code sample with XSL 1.0 (I can't use XSL 2.0 to be informed in advance).

XML Snippet : <root>
<3party>some_value</3party>
</root>

XSL Snippet : Say above value is in a XSL variable response.
<xsl:variable select="$response/root/3party" />


Transformation Exception I get :

javax.xml.transform.TransformerException: Syntax error in '$response/root/3party'.
at org.apache.xalan.xsltc.trax.TransformerFactoryImpl.passErrorsToListener(TransformerFactoryImpl.java:661)
at org.apache.xalan.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:836)
ERROR: Thu Nov 10 12:10:00 IST 2016.411 ErrorHandler.fatalError(TransformationConfig.java:46) Thread: Thread[main,5,main] - Compilation of XSL Stylesheet: with Error: Could not compile stylesheet
javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet at org.apache.xalan.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:832)

Answer

Xml elements are not allowed to start with a number. From here:

XML elements must follow these naming rules: - Element names are case-sensitive - Element names must start with a letter or underscore - Element names cannot start with the letters xml (or XML, or Xml, etc) - Element names can contain letters, digits, hyphens, underscores, and periods - Element names cannot contain spaces