Jahir
Jahir

Reputation: 365

Escape XML special characters on node name

I am creating xml from a map using DOM Parser. In my case the node may have white spaces or & (Special Char). But I need to create the node name by with escaping those characters. I there any ways that I could achieve this or any workaround to achieve this.

Upvotes: 0

Views: 1151

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328790

No. XML node names must adhere to the rules of the spec, specifically the Name rule:

[4]     NameStartChar      ::=      ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
[4a]    NameChar       ::=      NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5]     Name       ::=      NameStartChar (NameChar)*

Which means neither spaces nor & are valid in a node name.

The workaround is to use generic nodes with a name attribute:

<item name="name with special chars: &amp;">

Upvotes: 2

Related Questions