简体   繁体   English

XML模式:具有仅包含“受限”文本的属性的元素

[英]XML Schema: Element with attributes containing “restricted” text only

I want to define an element with some restricted text and an attribute 我想定义一个带有一些受限文本和一个属性的元素

<some_element my_attr="data">some restricted text</some_element>

How can I create an element like this? 如何创建这样的元素?

I tried: 我试过了:

<complexType>
    <simpleContent>
        <restriction base="string">
            <pattern value="AAA"></pattern>
            <attribute name="newattr" type="string"></attribute>
        </restriction>
    </simpleContent>
</complexType>

But getting error msg: 但是收到错误消息:

Complex Type Definition Representation Error for type '#AnonType_childparent'. 类型“ #AnonType_childparent”的复杂类型定义表示错误。 When is used, the base type must be a complexType whose content type is simple, or, only if restriction is specified, a complex type with mixed content and emptiable particle, or, only if extension is specified, a simple type. 使用时,基本类型必须是其内容类型为simple的complexType,或者(仅在指定了限制的情况下)是具有混合内容和可清空粒子的复杂类型,或者(仅在指定了扩展名的情况下)简单类型。 'string' satisfies none of these conditions. “字符串”不满足这些条件。

And then tried something like this 然后尝试这样的事情

<complexType>
    <complexContent>
        <extension base="string">
            <attribute name="attr" type="string"></attribute>
        </extension>
    </complexContent>
</complexType>

This time error was (This time I didn't add any restriction; this was for test purpose only): 这次错误是(这次我没有添加任何限制;这仅出于测试目的):

Complex Type Definition Representation Error for type '#AnonType_childparent'. 类型“ #AnonType_childparent”的复杂类型定义表示错误。 When is used, the base type must be a complexType. 使用时,基本类型必须为complexType。 'string' is a simpleType. 'string'是一个simpleType。

I am not getting clearly what the errors are implying? 我没有弄清楚这些错误意味着什么? Is there some better explanation of error text available? 有没有更好的错误文本说明?

Your question is more or less a duplicate of this question (just answered it). 您的问题或多或少是该问题的重复部分(刚刚回答了该问题)。

Think in these terms: 用以下术语思考:

  • if your element has attributes and/or nested elements, it must be a complex type. 如果您的元素具有属性和/或嵌套元素,则它必须是复杂类型。
  • if your element can have text, but no markup (nested elements) then it must be of simpleContent 如果您的元素可以有文本,但是没有标记(嵌套元素),那么它必须是simpleContent
  • simpleContent can only be extended from a simple type (if you need to add an attribute). simpleContent只能从简单类型扩展(如果需要添加属性)。 If you need additional constraints on the simple type, you need to specifically create a new simple type, restricting the base type, then extend that in a simple content. 如果您需要对简单类型的其他约束,则需要专门创建一个新的简单类型,限制基本类型,然后在简单内容中进行扩展。

This post on SO shows you an end-to-end example, of the above explanation. SO上的帖子向您展示了上述说明的端到端示例。 For your particular case: 对于您的特定情况:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:simpleType name="tSomeRestrictedText">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="AAA"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:element name="some_element">
        <xsd:complexType>
            <xsd:simpleContent>
                <xsd:extension base="tSomeRestrictedText">
                    <xsd:attribute name="my_attr" type="xsd:string"/>
                </xsd:extension>
            </xsd:simpleContent>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM