【问题标题】:How do you make an element in an XML Schema that allows any HTML element as its children?如何在 XML Schema 中创建一个允许任何 HTML 元素作为其子元素的元素?
【发布时间】:2009-05-31 16:37:29
【问题描述】:

我正在尝试在 XML 模式中创建一个元素,以便只有标准 (X)HTML 元素可以用作子元素。我试过的是这样的:

<xs:element name="description">
    <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:any namespace="http://www.w3.org/1999/xhtml" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

当然,这是行不通的,因为下面的 XML 没有明确指定命名空间:

<description>
    <p>this is a test</p>
    <p>this is a <b>bold</b> test</p>
    <h1>Those were the tests</h1>
</description>

我需要在文档中的某处指定命名空间,还是可以在架构中获取?

【问题讨论】:

    标签: html xml namespaces xsd


    【解决方案1】:

    我认为您需要禁用内容处理:

      <xs:any namespace="http://www.w3.org/1999/xhtml" processContents="skip"/>
    

    请参阅 XML Schema 规范中的 section 5.5(尤其是示例)

    【讨论】:

    • 我希望有比说“忽略它”更好的方法。
    • 但是你必须忽略它,因为它是无效的。在 XML 中使用正确的命名空间不是可选的。
    • 我希望架构可以告诉 xml 文档名称空间是什么。如果我要明确说明命名空间,我会在哪里做呢?
    【解决方案2】:

    您的架构看起来不错。请注意,xs:any/@processContents 的默认值是严格的,这意味着您的 XHTML 元素也将被验证,因此您还需要一个 XHTML 模式并将其从您的模式中导入。您可以在 xs:any 中使用 processContents="lax" 来指定只有当这些元素存在架构时才会应用验证。

    您的问题在于您应该为 XHTML 元素指定名称空间。您可以将 XHTML 命名空间声明为每个元素的默认命名空间,例如

    <p xmlns="http://www.w3.org/1999/xhtml">this is a test</p>
    

    或者您可以声明它绑定到一个前缀,例如 h,然后使用该前缀来限定您的 XHTML 元素:

    <description xmlns:h="http://www.w3.org/1999/xhtml">
      <h:p>this is a test</h:p>
      <h:p>this is a <b>bold</b> test</h:p>
      <h:h1>Those were the tests</h:h1>
    </description>
    

    DTD 不知道命名空间,并且命名空间声明只是属性,因此可以在元素上声明一个固定的 xmlns 属性以自动将其放入特定的命名空间。 XML Schema 可以识别命名空间,您不能将命名空间声明作为固定属性。

    【讨论】:

      【解决方案3】:

      我觉得你真的需要view this page

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-30
        • 1970-01-01
        • 2021-03-21
        相关资源
        最近更新 更多