有什么方法可以让架构在其结构中使用用户定义的元素吗?

发布于 2024-08-23 05:10:50 字数 1032 浏览 10 评论 0原文

我想知道是否可以让模式使用用户定义的元素(来自 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
  • Question

但不知道用户定义的子元素。 它将理解元素如何相互嵌套并具有其他限制(它将像枚举一样对待用户定义的值,以防止拼写错误并防止每个 subjectType 出现重复的 QuestionType)。

我将如何在架构中强制执行这种类型的用户定义内容?

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

谢谢

I'm wondering if it's possible to have a schema use user-defined elements (from the XML) for its structure.

As in, as a user, I would be able to have something like:

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

So the schema would know about:

  • subjectType
  • questionType
  • subjectInterview
  • question

but not the user-defined sub-elements.
It would understand how the elements nest in relation to each other and have other restrictions (it would treat the user-defined values like enums to prevent typos and prevent duplicate questionType's per subjectType).

How would I go about getting this type of user-defined content to be enforced in a schema?

edit: If it matters, I also plan on unmarshalling the user-defined xml to java classes.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2024-08-30 05:10:50

好的 - 发现我必须在架构中使用“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>

对于尝试做同样事情的人来说应该是一个不错的基础。

Ok - found out that I have to use "key" and "keyref" in the schema.

First- a modified XML file:

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

and here's the schema:

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

Should be a decent base for people trying to do the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文