这是一个有效的XML吗?

时间:2009-09-05 16:37:03

标签: xml schema xsd dtd standards-compliance

我觉得这个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>

4 个答案:

答案 0 :(得分:6)

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

Schema是一个规范,说明您正在使用哪个元素以及另一个元素。您可以使用DTD,XSD(W3C Schema),Relax NG等来指定架构。

答案 1 :(得分:2)

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

没有任何关于estate_price.price_suggestion作为XML规范禁止的元素名称,但是您的模式可能会对您的文档内容和结构施加约束,该约束不允许该元素(或任何其他元素)放在原处。

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

您的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值限制为有效的XML名称,该名称只能以_A-Za-z开头(不是'0-9',' - '或'。')。因此,尽管您的XML格式正确,但使用数字作为ID可能是一个糟糕的主意。正如已经指出的那样,如果你有一个DTD或模式,那么id可能被强制为ID类型,这会导致验证失败。

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

相关问题