本文介绍了如何引用其他 XSD 命名空间中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个跨两个 .XSD 文件定义的 complexType.

I have a complexType that's defined across two .XSD files.

Parent.xsd:

Parent.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:Parent"
     xmlns:emb="urn:Embedded" targetNamespace="urn:Parent" elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xs:import namespace="urn:Embedded" schemaLocation="Embedded.xsd"/>

    <xs:element name="ParentType">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="embedded" type="emb:EmbeddedType"></xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

嵌入式.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:Embedded" targetNamespace="urn:Embedded"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xs:complexType name="EmbeddedType">
        <xs:sequence>
            <xs:element name="numeric" type="xs:int"></xs:element>
            <xs:element name="embedded" type="EmbeddedType"
                minOccurs="0" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

我的 XML 看起来像这样:

And my XML looks something like this:

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

    <ParentType xmlns="urn:Parent" xmlns:emb="urn:Embedded">
        <embedded>
            <emb:numeric>12</emb:numeric>
            <emb:embedded>
                <emb:numeric>5</emb:numeric>
            </emb:embedded>
        </embedded>
    </ParentType>

有没有办法改变事物,使 embedded 元素具有相同的前缀?由于外部原因,我不能将 EmbeddedType 复制到 Parent.xsd 中.此外,不加选择地消除 emb 前缀的所有使用的解决方案将不起作用.

Is there a way to alter things so that both embedded elements have the same prefix? I can't just copy the EmbeddedType into Parent.xsd for external reasons. Further, a solution that eliminates all usage of the emb prefix indiscriminately will not work.

推荐答案

如果您希望第一个 embedded 与第二个 urn:Embedded 命名空间一起位于 urn:Embedded 命名空间中code>embedded,你可以通过xs:element/@ref:

If you want the first embedded to be in the urn:Embedded namespace along with the second embedded, you can move it there via xs:element/@ref:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:Parent"
           xmlns:emb="urn:Embedded"
           targetNamespace="urn:Parent"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

  <xs:import namespace="urn:Embedded" schemaLocation="Embedded.xsd"/>

  <xs:element name="ParentType">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="emb:embedded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

嵌入式.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:emb="urn:Embedded"
           targetNamespace="urn:Embedded"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

  <xs:element name="embedded" type="emb:EmbeddedType"/>

  <xs:complexType name="EmbeddedType">
    <xs:sequence>
      <xs:element name="numeric" type="xs:int"/>
      <xs:element name="embedded" type="emb:EmbeddedType"
                  minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

XML

<?xml version="1.0" encoding="utf-8"?>
<ParentType xmlns="urn:Parent"
            xmlns:emb="urn:Embedded"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:Parent Parent.xsd">
  <emb:embedded>
    <emb:numeric>12</emb:numeric>
    <emb:embedded>
      <emb:numeric>5</emb:numeric>
    </emb:embedded>
  </emb:embedded>
</ParentType>

这篇关于如何引用其他 XSD 命名空间中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:34