【问题标题】:Is this a valid XML?这是一个有效的 XML 吗?
【发布时间】:2010-11-25 21:43:25
【问题描述】:

我感觉这个 XML 无效,有人能解释一下原因吗?

我认为元素名称中的点有什么作用?

estate_price.price_suggestion

关于这个 XML 还有什么无效的吗?

XML

 \\ <?xml version="1.0" encoding="UTF-8"?>

<iad>
  <DataTag>
    <element id="0">
      <changed_string>content</changed_string>
      <no_of_bedrooms>content</no_of_bedrooms>
      <published_string>content</published_string>
      <mmo>content</mmo>
      <postcode>content</postcode>
      <utmx>content</utmx>
      <utmy>content</utmy>
      <disposed>content</disposed>
      <property_type>content</property_type>
      <isprivate>content</isprivate>
      <heading>content</heading>
      <published>content</published>
      <estate_price.price_suggestion>content</estate_price.price_suggestion>
      <ownership_type>content</ownership_type>
      <estate_size.useable_area>content</estate_size.useable_area>
      <adid>content</adid>
      <address>content</address>
      <sqmtrprice>content</sqmtrprice>
      <estate_size.primary_room_area>content</estate_size.primary_room_area>
      <location>content</location>
      <changed>content</changed>
      <orgname>content</orgname>
    </element>
    <element id="1">
      <changed_string>content</changed_string>
      <no_of_bedrooms>content</no_of_bedrooms>
      <published_string>content</published_string>
      <mmo>content</mmo>
      <postcode>content</postcode>
      <utmx>content</utmx>
      <utmy>content</utmy>
      <disposed>content</disposed>
      <property_type>content</property_type>
      <isprivate>content</isprivate>
      <heading>content</heading>
      <published>content</published>
      <estate_price.price_suggestion>content</estate_price.price_suggestion>
      <ownership_type>content</ownership_type>
      <estate_size.useable_area>content</estate_size.useable_area>
      <adid>content</adid>
      <address>content</address>
      <sqmtrprice>content</sqmtrprice>
      <estate_size.primary_room_area>content</estate_size.primary_room_area>
      <location>content</location>
      <changed>content</changed>
      <orgname>content</orgname>
    </element>
  </DataTag>
</iad>

【问题讨论】:

    标签: xml schema xsd dtd standards-compliance


    【解决方案1】:

    您的 XML 格式正确,应该在任何非验证 XML 解析器中进行解析。比如我用过XOM(来自http://www.xom.nu

            try {
            new nu.xom.Builder().build(new StringReader(s));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("OK");
    

    但是,有一些 XML 工具会对属性类型做出假设。 id 属性可能被假定为 ID 类型。此类型将 id 值限制为只能以 _A-Za-z 开头的有效 XML 名称(不是 '0-9'、'-' 或 '.')。因此,尽管您的 XML 格式良好,但使用数字作为 id 可能不是一个好主意。正如已经指出的那样,如果您有 DTD 或架构,则 id 可能已被强制为 ID 类型,这将导致验证失败。

    从您的帖子中不清楚您是否已经遇到问题 - 如果是这样,发布错误消息可能会有所帮助。

    【讨论】:

      【解决方案2】:

      Dave Markle 是正确的,因为您的 XML 序言不应该有反斜杠前缀(另外,请注意它是可选的,因为它只提供默认序言值)。

      至于元素名称中的点,如果您转到to the XML spec for start tags,您会看到它包含一个名称,该名称本身由一个NameStartChar 和一系列NameChar 组成。 NameChar 集合恰好包含字符“.”,因此标签名称中包含 . 是完全有效的,只要它不是第一个字符。

      【讨论】:

        【解决方案3】:

        我们需要您验证的架构来告诉您您的文档是否有效。

        没有关于 Estate_price.price_suggestion 作为 XML 规范禁止的元素名称的任何内容,但是您的架构可能会对您的文档内容和结构设置一个约束,不允许该元素(或任何其他元素)存在放在它所在的地方。

        【讨论】:

          【解决方案4】:

          良好的 XML 文档有两个级别:well-formed 和有效。格式良好意味着您符合 XML 标准,有效意味着您符合模式。

          Schema 是对您正在使用的元素以及可以进入另一个元素的规范。您可以使用 DTD、XSD(W3C 架构)、Relax NG 等来指定架构。

          【讨论】:

            猜你喜欢
            • 2014-03-25
            • 2016-11-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-09-30
            • 2012-11-17
            • 2020-03-19
            • 2020-05-29
            相关资源
            最近更新 更多