【问题标题】:Extend XSD with custom attributes? [duplicate]使用自定义属性扩展 XSD? [复制]
【发布时间】:2016-07-17 07:10:25
【问题描述】:

有没有办法用自定义属性扩展 XSD 元素?

例如,我想在 XSD 中执行以下操作:

<xs:element name="myElement" type="xs:string" myCustomAttribute="true" />

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    不,您不能在不混淆 XSD 处理器的情况下将自己的组件添加到 XSD。

    例如,Xerces-J,在遇到您的自定义属性示例时,

    <xs:element name="myElement" type="xs:string" myCustomAttribute="true" />
    

    将响应以下错误

    [错误] try.xsd:3:59: s4s-att-not-allowed: 属性 'myCustomAttribute' 不能出现在元素 'element' 中。

    如果您想扩充 XSD,请使用 xsd:annotation/xsd:appinfo 或您自己的命名空间中的属性 [Credit: @SpatialBridge]:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:myns="http://www.mycompany.com">
      <xs:element name="myElement" myns:myCustomAttribute="true"/>
    </xs:schema>
    

    【讨论】:

    • XMLSpy 也会产生错误。但是,使用命名空间限定属性似乎有效:&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myns="http://www.mycompany.com" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt; &lt;xs:element name="myElement" myns:myCustomAttribute="true"&gt; &lt;/xs:element&gt; &lt;/xs:schema&gt;
    • 是的,这也行。好主意。答案已更新。
    • 那为什么有效呢?我一直无法找到回答这个问题的文档。似乎自定义属性实际上不需要在自定义命名空间中定义。例如,如果我 import 我自己的命名空间,如果该属性未定义或具有 type="xs:integer",则不会引发错误。
    • 这是有效的,因为“Schema for Schemas”明确指出您可以在自己的命名空间中添加属性:顶级元素和本地元素允许像这样的额外属性:。此外,规范正文在此处声明“具有非模式命名空间的任何属性”:w3.org/TR/xmlschema-1/#declare-element
    • 不客气!属性名称中有一个拼写错误(i 而不是 o),因此它不会得到验证(行为松散)。您需要根据 xsi:schemaLocation 中提到的模式 + 您的自定义命名空间模式的模式将模式作为常规 XML 文件(而不是 XML 模式文件)进行验证。如果属性声明在范围内,则 processContents=lax 应该触发验证。就我而言,我收到了预期的错误“元素'xs:element'上的属性'myns:myCustomAttribute'的值'true'相对于其类型'integer'无效”我希望这会有所帮助!
    【解决方案2】:

    XBRL 是使用自己的自定义属性扩展 XML Schema 的技术示例。

    以下示例直接取自XBRL 2.1 specification。 xbrli:balance 和 xbrli:periodType 是 XBRL 在 XML Schema 之上添加的。

    <element
      id="a2"
      name="fixedAssets"
      xbrli:balance="debit"
      xbrli:periodType="instant"
      type="xbrli:monetaryItemType"
      substitutionGroup="xbrli:item"/>
    

    不过,正如 kjhughes 的回答中所述,您需要使用自己的命名空间(在这种情况下,xbrli 前缀与 http://www.xbrl.org/2003/instance 绑定)。

    【讨论】:

      【解决方案3】:

      使用自定义属性扩展 XSD 可以通过首先在自己的命名空间中定义自定义属性来完成,如下所示:

      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                 targetNamespace="http://www.mycompany.com" 
                 elementFormDefault="qualified" attributeFormDefault="unqualified">
          <xs:attribute name="myAttribute" type="xs:boolean" default="true"/>
      </xs:schema>
      

      在此命名空间http://www.mycompany.com 中,定义了一个名为myAttribute 的属性,其类型为xs:boolean

      接下来,在您的目标架构中使用此命名空间,如下所示:

      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xmlns:mc="http://www.mycompany.com" 
                 xsi:schemaLocation="http://www.mycompany.com ./doc.xsd" 
                 elementFormDefault="qualified" attributeFormDefault="unqualified">
          <xs:element name="element1" mc:myAttribute="false"/>
      </xs:schema>
      

      在此示例中,&lt;schema&gt; 元素包含定义自定义命名空间 (xmlns:mc="http://www.mycompany.com") 和自定义架构文件位置 (xsi:schemaLocation="http://www.mycompany.com ./doc.xsd") 的属性。

      目标架构包含单个元素"element1",并具有上面定义的自定义属性myAttribute,其值为"false"。请注意,自定义属性的名称以自定义命名空间前缀为前缀。另请注意,如果使用了无效类型的值(例如:mc:myAttribute="invalid"),则会产生验证错误。

      感谢@GhislainFourny 和@kjhughes 为这个答案提供帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-24
        • 1970-01-01
        • 1970-01-01
        • 2013-08-09
        • 2021-06-26
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多