Case conventions on element names?

前端 未结 10 1696
予麋鹿
予麋鹿 2020-12-02 18:00

Are there any formal recommendations on element casing in XML?

I know XHTML uses lowercase element names (as opposed to HTML which canonically uses uppercas

相关标签:
10条回答
  • 2020-12-02 18:38

    To expand on my comment above: the use of 'lowercase with hyphens' has some problems in XSLT. Specifically it is easy to confuse a node called, say, 'year-from-age' with a formula 'year - age' (e.g. subtract age from year).

    As @KarlKieninger points out, this is only a problem at the human level and not for the XSLT parser. However since this will often not produce an error, using 'lowercase with hyphens' as a standard is asking for trouble, IMHO.

    Some pertinent examples:

    <a>1</a><b>1</b>
    <xsl:value-of select="a+b"/>
    outputs 2, as expected
    
    <a>1</a><b>1</b>
    <xsl:value-of select="a-b"/>
    DOES NOT ERROR, BUT OUTPUTS NOTHING AT ALL
    

    In the above code, you must put at least one space before a subtraction operator, but there is no such requirement for an addition operator.

    <a-b>1</a-b><c>1</c>
    <xsl:value-of select="a-b -c"/>
    outputs 0, as expected
    

    But note how confusing the above is to read!

    <a>1</a><a-b>3</a-b><b>2</b>
    <xsl:value-of select="a-b"/>
    outputs 3
    
    <a>1</a><a-b>3</a-b><b>2</b>
    <xsl:value-of select="a -b"/>
    outputs -1
    

    The presence of a single space changes the output above, but neither variant is an error.

    0 讨论(0)
  • 2020-12-02 18:38

    Google's style guide recommends (perhaps even mandates) camelCase for all element names, as well as attribute names.

    0 讨论(0)
  • 2020-12-02 18:39

    The original intent for XML casing was lower case with hyphens. It's case sensitive and doesn't require you follow that convention -- so you can do whatever you want. I have no citations, sorry.

    0 讨论(0)
  • 2020-12-02 18:41

    Not that it matters, but I've always been partial to PascalCase for Elements and camelCase for attributes:

    <Root>
      <ParentElement attributeId="1">
        <ChildElement attributeName="foo" />
      </ParentElement>
    </Root>
    
    0 讨论(0)
提交回复
热议问题