13423

How to ensure an xml entity has a valid name

Question:

I have an app that generates a bunch of xml based off of some data from a source that I don't control. Basically I'm turning that data into an xml doc for serving via rss feeds. I may be getting data like

2_br=1 3_br=0

and I'm turning it into the following xml

<2_br>1</2_br> <3_br>0</3_br>

Obviously an xml tag can't start with a digit. I couldn't find anywhere a list of valid characters. Does it exists somewhere? I'm basically just going to prefix and invalid starting character or replace an inline invalid character with an _.

It's not hard to do, but basically I just cant find a list of invalid characters for an xml tag name.

Answer1:

How about this:<a href="http://www.w3.org/TR/REC-xml/#NT-S" rel="nofollow">Extensible Markup Language (XML) 1.0 (Fifth Edition)</a>

<em><strong>NameStartChar</strong></em> ::= ":" | [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]

Answer2:

XmlConvert.IsNCNameChar()

and

XmlConvert.IsStartNCNameChar()

can be used to validate a XML-tag name:

char[] validTagNameChars = pDesiredTagName.Where(ch => XmlConvert.IsNCNameChar(ch)).ToArray(); string validTagName = new string (validTagNameChars); if (validTagName == "" || !XmlConvert.IsStartNCNameChar(validTagName[0])) validTagName = "_" + validTagName;

Recommend

  • Is there an existing gem or script that converts numbers to comp-3/packed decimal format?
  • Python Floating Point Formatting
  • position: fixed with margin: auto in IE9/10
  • Colorize knockoutjs comment bindings in code editor
  • Separate float into digits
  • Search files(key) in s3 bucket takes longer time
  • custom string delimiters stringtemplate-4
  • What does Main.1 output file do in XCode?
  • Ruby and class variables in inherit class
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Most efficient way to move table rows from one table to another
  • Validate jQuery plugin, field not required
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • CakePHP ACL tutorial initDB function warnings
  • Loading .coffee files via a view in Rails
  • Unable to decode certificate at client new X509Certificate2()
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Groovy: Unexpected token “:”
  • How can I sort a a table with VBA with given text condition?
  • How to create a file in java without a extension
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • How to delay loading a property with linq to sql external mapping?
  • Using variable in a value field in jMeter
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • MySQL WHERE-condition in procedure ignored
  • When should I choose bucket sort over other sorting algorithms?
  • Release, debug version and Authorization Google?
  • Numpy divide by zero. Why?
  • Is there a mandatory requirement to switch app.yaml?
  • Traverse Array and Display in markup
  • Turn off referential integrity in Derby? is it possible?
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Add sale price programmatically to product variations
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?