如何创建和使用XML命名空间?

JSP*_*r01 9 xml namespaces xml-namespaces

我想要一个这样的页面:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:m="mine.xsd">
    <m:dialog m:title="Hello">Hi there!</m:dialog>
</html>
Run Code Online (Sandbox Code Playgroud)

我怎么写"mine.xsd"?

Bru*_*oLM 8

xsd文件是XML Schema文件,请阅读它.还有更多.

一个简单的例子:

XMLSchema1.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Types"
    targetNamespace="http://tempuri.org/"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/"
    xmlns:mstns="http://tempuri.org/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:simpleType name="Types">
    <xs:annotation>
      <xs:documentation>.NET types</xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:enumeration value="String" />
      <xs:enumeration value="Int16" />
      <xs:enumeration value="Int32" />
      <xs:enumeration value="Int64" />
      <xs:enumeration value="DateTime" />
      <xs:enumeration value="Double" />
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="DataSize">
    <xs:annotation>
      <xs:documentation>Number of bytes of the data</xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:int" />
  </xs:simpleType>

  <!-- ... -->

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

然后在您的XML文件中,您可以使用:

<?xml version="1.0" encoding="utf-8" ?>

<ValueSet
  xmlns="http://tempuri.org/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://tempuri.org/ XMLSchema1.xsd">

  <Values>
    <Value Name="Stats" Type="Int32" DataSize="4" />
    <Value Name="Time" Type="DateTime" DataSize="4" />
    <Value Name="Some" Type="Double" DataSize="4" />
    <Value Name="Other" Type="Double" DataSize="4" />
  </Values>

</ValueSet>
Run Code Online (Sandbox Code Playgroud)

  • 我想要的是在XHTML代码中嵌入一些自定义标签或属性,而不会导致语法错误.有什么办法可以使用XML模式将我的标签"注入"`<html>`元素? (2认同)