Case insensitivity in selectors?

2019-04-07 11:58发布

问题:

I am trying to use jQuery for XML processing. One of the problem that I am stuck with jQuery is it is case insensitive in processing tags and attribute.

For e.g., consider the following code:

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

the output we get is:

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

whereas the output i am looking for is:

<Book ISBN="1234">Some title</Book>

Any possibility? (Note that "B" is capital letter, and the whole attribute name "ISBN" is also in capital case, whereas the jQuery html output is completely lower case) Please help.

回答1:

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 are getting is correct. To my knowledge, jQuery core can't create an HTML document, where case sensitivity matters for element and attribute names.

EDIT: See below. I had originally said jQuery can't create an XML document where case sensitivity matters. Clearly, it can. But it can't preserve the case if you're injecting into HTML. For a solution, see: jQuery converting XML tags to uppercase



回答2:

the problem would be the .html() ... html in itself should be lowercase so jquery jsut return the "Valid" html format. if u need to parse xml i am sure theres librairy to do it that will keep the Case of your Xml.

personally i would try parsexml or any of the library you could find with a quick search

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



回答3:

My problem was the XML 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 had no control over how it was being output, and originally 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(); it made it possible to go through each, the problem came when I fed that data to $(xml).find('event').filter(function(){ ....

Once it went through .find and .filter all of the cameCasing was lost, and made a lot of issues for things that relied on camel casing.

So the simple fix was like others mentioned above:

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

Simply by using $.parseXML it converted that text to a valid XML document that didn't lose camelCasing.



标签: jquery xml case