Skip to main content

Trying to read XML in the following format with “:” is throwing Can't parse XML element names containing colon ':' error.

<sh:root>
<sh:book> </sh:book>
<sh:genre> </sh:genre>
<sh:id> </sh:id>
<sh:book> </sh:book>
<sh:genre> </sh:genre>
<sh:id> </sh:id>
<sh:book> </sh:book>
<sh:genre> </sh:genre>
<sh:id> </sh:id>
</sh:root>

What’s the best way to read such XML? (Note that changing “:” to “_” in the XML works.)

An XML qualified name is in the form prefix:localPart where prefix is a namespace prefix and localPart is the actual name of the element. (For more details on this, see Namespace Constraint: Prefix Declared.)

For instance, the XML Parser will correctly parse the following XML as long as it has the namespace declaration in the sh:root element:

<sh:root xmlns:sh="urn:dummy">
<sh:book>a</sh:book>
<sh:genre>b</sh:genre>
<sh:id>c</sh:id>
<sh:book>d</sh:book>
<sh:genre>e</sh:genre>
<sh:id>f</sh:id>
<sh:book>g</sh:book>
<sh:genre>h</sh:genre>
<sh:id>i</sh:id>
</sh:root>

So the solution is to either remove the sh: namespace prefix or add the namespace declaration.


Reply