使用自定义属性扩展 XSD?
Extend XSD with custom attributes?
有没有办法用自定义属性扩展 XSD 元素?
例如,我想在 XSD 中执行以下操作:
<xs:element name="myElement" type="xs:string" myCustomAttribute="true" />
不,您不能将自己的组件添加到 XSD 而不会混淆 XSD 处理器。
例如,Xerces-J,在遇到您的自定义属性示例时,
<xs:element name="myElement" type="xs:string" myCustomAttribute="true" />
将响应以下错误:
[Error] try.xsd:3:59: s4s-att-not-allowed: Attribute
'myCustomAttribute' cannot appear in element 'element'.
如果您想扩充 XSD,请使用 xsd:annotation/xsd:appinfo
or attributes from your own namespace [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>
XBRL 是使用自己的自定义属性扩展 XML 模式的技术示例。
下面的例子直接取自XBRL 2.1 specification。 xbrli:balance 和 xbrli:periodType 由 XBRL 在 XML 架构之上添加。
<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 绑定)。
使用自定义属性扩展 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>
在此示例中,<schema>
元素包括定义自定义命名空间 (xmlns:mc="http://www.mycompany.com"
) 的属性,以及自定义架构文件 (xsi:schemaLocation="http://www.mycompany.com ./doc.xsd"
) 的位置。
目标架构包含单个元素 "element1"
,并具有上面定义的自定义属性 myAttribute
,其值为 "false"
。请注意,自定义属性的名称以自定义命名空间前缀为前缀。另请注意,如果使用了无效类型的值(例如:mc:myAttribute="invalid"
),将生成验证错误。
感谢@GhislainFourny 和@kjhughes 对这个答案的帮助。
有没有办法用自定义属性扩展 XSD 元素?
例如,我想在 XSD 中执行以下操作:
<xs:element name="myElement" type="xs:string" myCustomAttribute="true" />
不,您不能将自己的组件添加到 XSD 而不会混淆 XSD 处理器。
例如,Xerces-J,在遇到您的自定义属性示例时,
<xs:element name="myElement" type="xs:string" myCustomAttribute="true" />
将响应以下错误:
[Error] try.xsd:3:59: s4s-att-not-allowed: Attribute 'myCustomAttribute' cannot appear in element 'element'.
如果您想扩充 XSD,请使用 xsd:annotation/xsd:appinfo
or attributes from your own namespace [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>
XBRL 是使用自己的自定义属性扩展 XML 模式的技术示例。
下面的例子直接取自XBRL 2.1 specification。 xbrli:balance 和 xbrli:periodType 由 XBRL 在 XML 架构之上添加。
<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 绑定)。
使用自定义属性扩展 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>
在此示例中,<schema>
元素包括定义自定义命名空间 (xmlns:mc="http://www.mycompany.com"
) 的属性,以及自定义架构文件 (xsi:schemaLocation="http://www.mycompany.com ./doc.xsd"
) 的位置。
目标架构包含单个元素 "element1"
,并具有上面定义的自定义属性 myAttribute
,其值为 "false"
。请注意,自定义属性的名称以自定义命名空间前缀为前缀。另请注意,如果使用了无效类型的值(例如:mc:myAttribute="invalid"
),将生成验证错误。
感谢@GhislainFourny 和@kjhughes 对这个答案的帮助。