XML Doctype,如何申请 [英] XML Doctype, how to apply

查看:261
本文介绍了XML Doctype,如何申请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在xml中应用文档类型?

How do I apply a doctype in xml?

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE application [
       <!ELEMENT application (#PCDATA)>
]>
<application>
       <settings>
              <environment use="production" />
              <database datasource="MySQL" environment="production" />
              <database datasource="MySQL" environment="development" />
              <import>
                     <path value="Application" />
                     <path value="Application/Library" />
              </import>
       </settings>
       <environment name="production">
              <database>
                     <hostname value="127.0.0.1" />
                     <username value="root" />
                     <password value="" />
                     <database value="app" />
              </database>
       </environment>
       <environment name="development">
              <database>
                     <hostname value="127.0.0.1" />
                     <username value="root" />
                     <password value="" />
                     <database value="app" />
              </database>
       </environment>
</application>

我还想验证XML中必须包含的元素和属性,例如,环境设置中,您必须具有此节点,以及如何创建可以由各种XML实现的单个doctype,以便如何使用xsd文件验证xml?

I also wanted to validate the elements that are necessary to contain and attributes in the XML, for example, environment settings, you must have this node, and also how I can create a single doctype that can be implemented by various XML, so how can I validate xml using xsd file?

推荐答案

尝试以下模式(您需要填充类型定义):

Try the following schema (you need to populate the type definitions):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://example.com/sample"
    xmlns="http://example.com/sample">
  <xs:element name="application">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="settings" type="settingsType" />
        <xs:element name="environment" maxOccurs="unbounded" type="environmentType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!-- type definitions -->
  <xs:complexType name="settingsType">
    ... define type here...
  </xs:complexType>
  <xs:complexType name="environmentType">
    ... define type here...
  </xs:complexType>
</xs:schema>

您希望验证的XML实例可以通过在您的XML中包含xmlns属性来与模式关联根节点:

The XML instance you wish to validate can then be associated with the schema by including a xmlns attribute in your root node:

<application xmlns="http://example.com/sample">
       <settings>
              ....
       </settings>
       <environment name="production">
              ....
       </environment>
       <environment name="development">
              ....
       </environment>
</application>

这篇关于XML Doctype,如何申请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆