TechBreak TechBreak -4 years ago 158
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>

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(
at org.apache.xalan.xsltc.trax.TransformerFactoryImpl.newTemplates(
ERROR: Thu Nov 10 12:10:00 IST 2016.411 ErrorHandler.fatalError( 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(

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download