【问题标题】:XML Schema: How do I allow an element with attributes but no text?XML Schema:如何允许具有属性但没有文本的元素?
【发布时间】:2015-04-12 07:09:57
【问题描述】:

我正在尝试创建一个 XSD 来验证 XML 文件,并且有几个元素需要具有属性但没有文本。

例如这应该被认为是有效的:

<DEPRECATED value="blah"/>

这些是无效的:

<DEPRECATED>blah</DEPRECATED>
<DEPRECATED value="blah">bad text</DEPRECATED>

我尝试按照here 的描述声明一个复杂的空元素,但要么我对示例的解释不正确,要么示例本身有误(请参阅下面的错误):

<xs:element name="DEPRECATED" type="stringValue" minOccurs="0" maxOccurs="1"/>

<xs:complexType name="stringValue">
        <xs:complexContent>
            <xs:restriction base="xs:integer">
                <xs:attribute name="value" type="xs:string"/>
            </xs:restriction>
        </xs:complexContent>
</xs:complexType>

错误:类型“stringValue”的复杂类型定义表示错误。使用时,基类型必须是 complexType。 'integer' 是一个简单类型。

我也尝试过这样定义 complexType:

<xs:complexType name="stringValue">
    <xs:attribute name="value" type="xs:string"/>
</xs:complexType>

但这认为上面的无效示例是有效的。 [编辑:更正,上述确实有效。]

我也尝试过类似问题的答案(Define an XML element that must be empty and has no attributesPrevent Empty Elements in XML via XSD),但没有运气。

如何验证元素是否具有属性,但没有文本?

【问题讨论】:

  • Argh - 答案就在问题中。上面定义的 complexType 可以工作,我的 XML 文件有问题。

标签: xml xsd xsd-validation


【解决方案1】:

此 XML 将有效

<DEPRECATED value="blah"/>

还有这个 XML

<DEPRECATED>blah</DEPRECATED>

还有这个 XML

<DEPRECATED value="blah">bad text</DEPRECATED>

使用此 XSD 将无效:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="DEPRECATED">
    <xs:complexType>
      <xs:attribute name="value"/>
    </xs:complexType>
  </xs:element>

</xs:schema>

【讨论】:

  • 奇怪 - 当我尝试这个时,它仍然认为 bad text 是有效的(我没有尝试其他无效的情况)。 DEPRECATED 元素本身在属于另一个复杂类型的序列中是否重要?将在原始问题中添加详细信息...
  • 不,没关系。如果您仍然遇到问题,请提供确切 XSD 和 XML 以及错误消息,因为处于序列中,或者被匿名或显式定义无关紧要。谢谢。
  • 谢谢。我发现了问题 - 我的文件中已弃用的元素(我预计无效的元素)是另一个元素的子元素,而不是“属性”元素。
【解决方案2】:

我想你是在追求这样的事情

<xs:schema elementFormDefault="qualified" version="1.0"
    targetNamespace="stackoverflow" xmlns:tns="stackoverflow"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="Test">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="DEPRECATED" type="tns:deprecatedType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="deprecatedType">
        <xs:attribute name="number" />
    </xs:complexType>
</xs:schema>

此 XML 文档有效

<sf:Test xmlns:sf="stackoverflow"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="stackoverflow test.xsd">
    <sf:DEPRECATED number="123"/>
</sf:Test>

此 XML 文档无效

<sf:Test xmlns:sf="stackoverflow"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="stackoverflow test.xsd">
    <sf:DEPRECATED number="123">TEST</sf:DEPRECATED>
</sf:Test>

如果你想限制属性,你可能需要这样的东西:

<xs:complexType name="deprecatedType">
    <xs:attribute name="number">
        <xs:simpleType>
            <xs:restriction base="xs:string" />
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>

【讨论】:

  • 对不起。没有冒犯的意思。最初的问题似乎是试图对属性添加限制,因此试图展示如何做到这一点并将整个事情放在完整的上下文中。
  • 原来的评论好像被删除了。希望答案有帮助
  • 顺便说一句:我用你的嵌套 complexTypes 进行了测试并让它工作。您如何测试验证?
  • 你是对的,它确实有效。我文件中不推荐使用的元素(我预计无效的元素)是另一个元素的子元素,而不是“属性”元素。 #facepalm 我在删除之前看到了您所指的评论。不是我的:)。干杯。
【解决方案3】:

试试这个:

  <xsd:simpleType name="EmptyType">
    <xsd:restriction base="xsd:string">
      <xsd:length value="0" />
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:complexType name="DeprecatedType">
    <xsd:simpleContent>
      <xsd:extension base="EmptyType">
        <xsd:attribute name="value" type="xsd:string" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

【讨论】:

    猜你喜欢
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    相关资源
    最近更新 更多