Case insensitive selectors?

I am trying to use jQuery to process XML. One of the problems I'm stuck with in jQuery is case insensitivity when handling tags and attribute.

For example, consider the following code:

$("<div><Book ISBN='1234'>Some title</Book></div>").html() 

output:

 <book isbn="1234">Some title</book> 

whereas the conclusion I'm looking for is:

 <book isbn="1234">Some title</book> 

Any opportunity? (Note that β€œB” is an uppercase letter, and the entire attribute name β€œISBN” is also in the capital case, while the jQuery html output is completely lowercase). Please, help.

+7
source share
3 answers

According to http://www.w3.org/TR/CSS21/selector.html , in HTML, element names are case-insensitive, but in XML they are case-sensitive. The same is true for attribute names.

So, the HTML output you get is correct. As far as I know, the jQuery core cannot create an HTML document where case sensitivity is important for element names and attributes.

EDIT: see below. I initially said that jQuery cannot create an XML document in which case sensitivity is important. It is clear that this is possible. But this cannot save the case if you type in HTML. For a solution, see jQuery converting XML tags to uppercase

+6
source

the problem is that .html () ... html itself must be lowercase, so jquery jsut returns a "Valid" html format. if you need to parse xml, I am sure that theres librairy will do this, which will contain a case of your Xml.

I personally would try parsexml or any library you could find with a quick search

http://api.jquery.com/jQuery.parseXML/

+1
source

My problem was that the XML that I was pulling from another site output the actual XML data as encoded HTML ... IE:

 <status> <eventData> &lt;net id="District 3" name="District 3"&gt; &lt;updateTimestamp&gt;2014-04-16T22:15:42-05:00&lt;/updateTimestamp&gt; &lt;category&gt;Current&lt;/category&gt; </eventData> </status> 

So, I did not control how it is output, and initially I just used basic jQuery via ajax to get the XML, and then with the returned data

 $.get(eventDataURL, {}, function (rawXML) { var xml = $(rawXML).text(); } 

If I used $(rawxml).text(); , he allowed to go through each, a problem arose when I submitted this data to $(xml).find('event').filter(function(){ ....

As soon as he went through .find and .filter , all the events received were lost and made a lot of problems for things that relied on the camel body.

So, a simple fix was similar to the others mentioned above:

 $.get(eventDataURL, {}, function (rawXML) { var xmlText = $(rawXML).text(); xml = $.parseXML(xmlText); } 

Just using $.parseXML , it converts this text into a valid XML document that camelCasing has not lost.

0
source

All Articles