XHTML5 and HTML4 character entities

前端 未结 5 2082
我寻月下人不归
我寻月下人不归 2020-12-09 11:57

Does XHTML5 support character entities such as   and . At work we can require specific software to access the admin side of the

相关标签:
5条回答
  • 2020-12-09 12:16

    I needed an XML validation of potentially HTML 5. HTML 4 and XHTML only had a mediocre 250 or so entities, while the current draft (January 2012) has more than 2000.

    GET 'http://www.w3.org/TR/html5-author/named-character-references.html' |
    xmllint --html --xmlout --format --noent - | 
    egrep '<code|<span.*glyph' |  # get only the bits we're interested in
    sed -e 's/.*">/__/' | # Add some "__" markers to make e.g. whitespace
    sed -e 's/<.*/__/' |  #  entities work with xargs
    sed 's/"/\&quot;/' | # xmllint output contains " which messes up xargs
    sed "s/'/\&apos;/" | # ditto apostrophes. Make them HTML entities instead.
    xargs -n 2 echo |  # Put the entity names and values on one line
    sed 's/__/<!ENTITY /' | # Make a DTD
    sed 's/;__/ /' |
    sed 's/ __/"/'  |
    sed 's/__$/">/' |
    egrep -v '\bapos\b|\bquot\b|\blt\b|\bgt\b|\bamp\b' # remove XML entities.
    

    You end up with a file containing 2114 entities.

    <!ENTITY AElig "&#xC6;">
    <!ENTITY Aacute "&#xC1;">
    <!ENTITY Abreve "&#x102;">
    <!ENTITY Acirc "&#xC2;">
    <!ENTITY Acy "&#x410;">
    <!ENTITY Afr "&#x1D504;">
    

    Plugging this into an XML parser should allow the XML parser to resolve these character entities.

    Update October 2012: Since the working draft now has a JSON file (yes, I'm still using regular expressions) I worked it down to a single sed:

    curl -s 'http://www.w3.org/TR/html5-author/entities.json' |
    sed -n '/^  "&/s/"&\([^;"]*\)[^0-9]*\[\([0-9]*\)\].*/<!ENTITY \1 "\&#\2;">/p' |
    uniq
    

    Of course a javascript equivalent would be a lot more robust, but not everyone has node installed. Everyone has sed, right? Random sample output:

    <!ENTITY subsetneqq "&#10955;">
    <!ENTITY subsim "&#10951;">
    <!ENTITY subsub "&#10965;">
    <!ENTITY subsup "&#10963;">
    <!ENTITY succapprox "&#10936;">
    <!ENTITY succ "&#8827;">
    
    0 讨论(0)
  • 2020-12-09 12:20

    There is no DTD for XHTML5, so an XML parser will see no entity definitions (other than the predefined ones). If you wanted to use an entity you would have to define it for yourself in the internal subset.

    <!DOCTYPE html [
        <!ENTITY mdash "—">
    ]>
    <html xmlns="http://www.w3.org/1999/xhtml">
        ... &mdash; ...
    </html>
    

    (Of course using the internal subset is likely to trip browsers up if you serve it to them as text/html. Sending an internal subset in a non-XHTML HTML5 document is disallowed.)

    The HTML5 wiki currently recommends:

    Do not use entity references in XHTML (except for the 5 predefined entities: &amp;, &lt;, &gt;, &quot; and &apos;)

    And I agree with this advice not just for XHTML5 but for XML and HTML in general. There's little reason to be using the HTML entities for anything today. Unicode characters typed directly are far more readable for everyone, and &#...; character references are available for those sad cases when you can't guarantee a 8-bit/encoding-clean transport. (Since HTML entities are not defined for the majority of Unicode characters, you are going to need those anyway.)

    0 讨论(0)
  • 2020-12-09 12:25

    The right answer (the modern way)

    I asked this question five years ago. Now every browser supports UTF-8. And, every inception of UTF-8 includes glyph support for all named character entities. The rightmost current solution to this problem is not to use named entities at all but to serve only UTF-8 (strict) and to use actually characters in that.

    This is a list of all XML entities. All of these have UTF-8 character alternatives -- and that's how they'd normally be rendered anyway.

    For instance, take

    U+1D6D8, MATHEMATICAL BOLD SMALL CHI            , b.chi
    

    I suppose in some variant of xml you could have &b.chi or something, searching for MATHEMATICAL BOLD SMALL CHI you'll find some page on fileformat.info, which has the

    0 讨论(0)
  • 2020-12-09 12:34

    My best advice is to not upgrade to HTML5 or XHTML5 until support for character entity names is provided.

    Anyone who thinks that &#12345; makes more sense than &mdash; needs a brain upgrade. Most people can't remember huge tables of numbers.

    Those of us who have to remain with older operating systems to be compatible with existing scientific, real-time, or point-of-sale hardware (or government networks) can't just type the character or pick it from a list. It won't save correctly in the file.

    The reason this has been imposed on us is that w3c no longer wants the expense of serving DTD files, so we must go back to the stone age.

    Nothing like this that has been provided should ever be deprecated.

    0 讨论(0)
  • 2020-12-09 12:38

    Using the following answer: https://stackoverflow.com/a/9003931/689044 , I created the file and posted it as a Gist on GitHub: https://gist.github.com/cerkit/c2814d677854308cef57 for those of you who need the Entities in a file.

    I used it successfully with ASP.NET MVC by loading the text file into the Application object and using that value with my (well-formed) HTML to parse a System.Xml.XmlDocument.

    XmlDocument doc = new XmlDocument();
    
    // load the HTML entities into the document and add a root element so it will load
    // The HTML entities are required or it won't load the document if it uses any entities (ex: &ndash;)
    doc.LoadXml(string.Format("{0}<root>{1}</root>", Globals.HTML_ENTITIES, control.HtmlText));
    var childNodes = doc.SelectSingleNode("//root").ChildNodes;
    // do your work here    
    foreach(XmlNode node in childNodes)
    {
        // or here
    }
    

    Globals.HTML_ENTITIES is a static property that loads the entities from the text file and stores them in the Application object, or it uses the values if they're already loaded in the Application object.

    public static class Globals
    {   
        public static readonly string APPLICATION_KEY_HTML_ENTITIES = "HTML_ENTITIES";
    
        public static string HTML_ENTITIES
        {
            get
            {
                string retVal = null;
                // load the HTML entities from a text file if they're not in the Application object
                if(HttpContext.Current.Application[APPLICATION_KEY_HTML_ENTITIES] != null)
                {
                    retVal = HttpContext.Current.Application[APPLICATION_KEY_HTML_ENTITIES].ToString();
                }
                else
                {
                    using (StreamReader sr = File.OpenText(HttpContext.Current.Server.MapPath("~/Content/HtmlEntities/RootHtmlEntities.txt")))
                    {
                        retVal = sr.ReadToEnd();
                        HttpContext.Current.Application[APPLICATION_KEY_HTML_ENTITIES] = retVal;
                    }
                }
    
                return retVal;
            }
        }
    }
    

    I tried creating a long string to hold the values, but it kept crashing Visual Studio, so I decided that the best route would be to load the text file at runtime and store it in the Application object.

    0 讨论(0)
提交回复
热议问题