任何方式让架构在其结构中使用用户定义的元素?

时间:2010-02-24 20:03:18

标签: xml schema xsd user-input

我想知道是否有可能让架构使用用户定义的元素(来自XML)来构造它。

作为用户,我可以拥有类似的内容:

<subjectType>
   <name>plane</name>
   <name>bird</name>
</subjectType>
<questionType>
   <name>organic</name>
   <name>awesome</name>
</questionType>
<subjectInterview subjectType="plane">
   <question questionType="organic">false</question>
   <question questionType="awesome">true</question>
</subjectInterview>
<subjectInterview subjectType="bird">
   <question questionType="organic">true</question>
   <question questionType="awesome">false</question>
</subjectInterview>

因此架构会知道:

  • subjectType
  • questionType
  • subjectInterview
  • 问题

但不是用户定义的子元素。 它将理解元素如何相互嵌套并具有其他限制(它会将用户定义的值视为枚举以防止打字错误并防止每个subjectType重复的questionType)。

我如何才能在模式中实现这种类型的用户定义内容?

编辑:如果重要,我还计划将用户定义的xml解组为java类。

由于

1 个答案:

答案 0 :(得分:0)

好的 - 发现我必须在架构中使用“key”和“keyref”。

首先是修改后的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <subjectType>
       <subject idref="plane"/>
       <subject idref="bird"/>
    </subjectType>
    <questionType>
        <question idref="organic"/>
        <question idref="awesome"/>
    </questionType>
    <subjectInterview id="plane">
       <question id="organic">false</question>
       <question id="awesome">true</question>
    </subjectInterview>
    <subjectInterview id="bird">
       <question id="organic">true</question>
       <question id="awesome">false</question>
    </subjectInterview>
</root>

这是架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="subjectType" type="subjectType" maxOccurs="unbounded"/>
                <xs:element name="questionType" type="questionType" maxOccurs="unbounded"/>
                <xs:element name="subjectInterview" type="subjectInterview" maxOccurs="unbounded">
                    <xs:key name="questionID">
                        <xs:selector xpath="./question"/>
                        <xs:field xpath="@id"/>
                    </xs:key>

                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:key name="subjectID">
            <xs:selector xpath="./subjectInterview"/>
            <xs:field xpath="@id"/>
        </xs:key>
        <xs:keyref name="subjectIDref" refer="subjectID">
            <xs:selector xpath="./subjectType/subject"/>
            <xs:field xpath="@idref"/>
        </xs:keyref>

        <xs:keyref name="questionIDref" refer="questionID">
            <xs:selector xpath="./questionType/question"/>
            <xs:field xpath="@idref"/>
        </xs:keyref>
    </xs:element>

    <xs:complexType name="subjectInterview">
        <xs:sequence  maxOccurs="unbounded">
            <xs:element name="question" type="question"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="question">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="id" type="xs:string"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="subjectType">
        <xs:sequence>
            <xs:element name="subject" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:attribute name="idref" type="xs:string"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="questionType">
        <xs:sequence>
            <xs:element name="question" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:attribute name="idref" type="xs:string"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

应该是尝试做同样事情的人的理想基础。