简体   繁体   English

XML 元素可以同时具有文本和元素内容吗?

[英]Can an XML element have both text and element content?

I have this XML element:我有这个 XML 元素:

<address>123 main street Chicago IL 60605</address>

Now let's say I want to add <country> element to it (and I insistent on country being an element not attribute), what would be right way to do that?现在假设我想向它添加<country>元素(并且我坚持认为 country 是一个元素而不是属性),那么正确的方法是什么?

I can imagine it like this:我可以这样想象:

<address>
<country>USA</country>
123 main street Chicago IL 60605
</address>

But the actual address above now looks somewhat tagless although I believe it's still a valid XML right?但是上面的实际地址现在看起来有点无标签,尽管我相信它仍然是有效的 XML 对吧?

I am asking because I haven't seen any example like above (in terms of learning XML format) where an element contains a child element but also an inline value as well.我之所以问,是因为我没有看到任何像上面这样的例子(在学习 XML 格式方面),其中一个元素包含一个子元素,但也包含一个内联值。

And I know country being an attribute here would make a lot more sense but my question is about this way of format.而且我知道国家作为一个属性在这里会更有意义,但我的问题是关于这种格式的方式。 If that's legal and acceptable or the actually address should be enclosed in another tag in later example like <street_address> ?如果这是合法且可接受的,或者实际地址应该包含在后面的示例中的另一个标签中,例如<street_address>

Can an XML element have both text and element content? XML 元素可以同时具有文本和元素内容吗?

Yes, it's called mixed content .是的,它被称为混合内容

Now let's say I want to add <country> element to it (and I insistent on country being an element not attribute), what would be right way to do that?现在假设我想向它添加<country>元素(并且我坚持认为 country 是一个元素而不是属性),那么正确的方法是什么?

It is up to you, as a designer of an XML format, to decide.作为 XML 格式的设计者,您可以自行决定。

I can imagine it like this:我可以这样想象:

 <address> <country>USA</country> 123 main street Chicago IL 60605 </address>

But the actual address above now looks somewhat tagless although I believe it's still a valid XML right?但是上面的实际地址现在看起来有点无标签,尽管我相信它仍然是有效的 XML 对吧?

Terminology clarification: What you imagine is well-formed XML.术语说明:您想象的是格式良好的XML。 Whether or not it is valid depends on the schema you design or adopt.它是否有效取决于您设计或采用的模式。 For further information on the distinction, see Is there any difference between 'valid xml' and 'well formed xml'?有关区别的更多信息,请参阅“有效 xml”和“格式良好的 xml”之间是否有任何区别?

As written above, the address element is said to have mixed content .如上所述, address元素被称为具有混合内容 Such a form is more common for document-oriented designs than data-oriented designs, but the XML is well-formed in either case.这种形式在面向文档的设计中比面向数据的设计更常见,但 XML 在任何一种情况下都是格式良好的。 If you're designing a schema, you'd probably want to markup the other components of an address,如果你正在设计一个模式,你可能想要标记地址的其他组成部分,

<address>
  <street>123 main street</street>
  <city>Chicago</city>
  <state>IL</state>
  <zip>60605</zip>
<address>

rather than using mixed content , which as mentioned is useful for documents:而不是使用混合内容,如前所述,这对文档很有用:

<p>This is an example of <i>mixed content</i></p>

As a design convention, it's best to use mixed content only where the text still makes sense if you remove the markup:作为设计约定,最好仅在删除标记后文本仍然有意义的情况下使用混合内容

<p>That is <emph>so</emph> interesting!</p>

or或者

    <bibref><authors><author format="surname, initials">Kay M. H.</author
></authors>: <title>XSLT Programmer's Reference</title
>. 4th edition, <publisher>Wiley</publisher>, <year>2010</year>.</bibref>

Some XML processing APIs only make sense if you follow this convention, for example XPath: contains(bibref, "Wiley, 2010") .一些 XML 处理 API 仅在遵循此约定时才有意义,例如 XPath: contains(bibref, "Wiley, 2010") (This query works whether or not the bibref has been "marked up".) (无论 bibref 是否已“标记”,此查询都有效。)

But people can and do use XML mixed content in other ways.但是人们可以并且确实以其他方式使用 XML 混合内容。 Mixed content is of course the biggest difference between XML and JSON, and reflects the fact that the M in XML means "markup": it was devised as a system for labelling parts of a textual document.混合内容当然是 XML 和 JSON 之间最大的区别,并反映了 XML 中的 M 表示“标记”的事实:它被设计为一个系统的文本标签文档部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM