【问题标题】:How to create and use XML namespace?如何创建和使用 XML 命名空间?
【发布时间】:2011-03-17 14:26:02
【问题描述】:

我想要这样的页面:

<?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>

如何写“mine.xsd”?

【问题讨论】:

    标签: xml namespaces xml-namespaces


    【解决方案1】:

    您可以自己手动编写该 XSD 文件 - 您只需研究构成 XML 模式的内容,并了解如何自己编写该代码。 Google 或 Bing 的“XML Schema Tutorial”应该会给你很多点击(例如 W3Schools XML Schema Tutorial)。

    或者您可以使用 Visual Studio 来执行此操作:

    Example image

    • 在 Visual Studio 中打开要处理的 XML 文件
    • XML 菜单中,选择Create Schema 菜单项

    这将从您的 XML 文件生成 XML 架构。

    注意:这是一个很好的起点 - 但绝不是完美的。尤其是对于较小的 XML 文件,生成过程无法知道很多事情,它只需要做出某些假设——这可能是正确的,也可能是错误的。您肯定需要查看 XML 模式文件 - 这就是第一个选项的诀窍非常方便发挥作用的地方!

    【讨论】:

      【解决方案2】:

      xsd 文件是 XML Schema 文件,read about itSome more here.

      一个简单的例子:

      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>
      

      然后在你的 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>
      

      【讨论】:

      • 我想要的是在 XHTML 代码中嵌入一些自定义标签或属性,而不会导致语法错误。有什么方法可以使用 XML 模式将我的标签“注入”到&lt;html&gt; 元素中?
      • @JSPDeveloper01:我认为在您的自定义 xsd 中使用 xsd:annotation/xsd:appinfo 可以像这样进行扩充:stackoverflow.com/a/36289758/1915920
      猜你喜欢
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多