问题描述
假设我有这个 XML:
Assuming that I have this XML:
<parameters>
<parameter type="string" isVisible="True" optional="False" id="DealerCode">
<DealerCode>ABCDEF001</DealerCode>
</parameter>
</parameters>
我使用 Xml-Schema 生成器生成了一个基本的生成器.结果如下:
I used a Xml-Schema generator to generate me a basic one. This results in this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="DealerCode" type="xs:string"/>
<xs:element name="parameter">
<xs:complexType>
<xs:sequence>
<xs:element ref="DealerCode"/>
</xs:sequence>
<xs:attribute type="xs:string" name="type"/>
<xs:attribute type="xs:string" name="isVisible"/>
<xs:attribute type="xs:string" name="optional"/>
<xs:attribute type="xs:string" name="id"/>
</xs:complexType>
</xs:element>
<xs:element name="parameters">
<xs:complexType>
<xs:sequence>
<xs:element ref="parameter"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
DealerCode元素的命名取决于parameter元素的identifier.例如,如果 parameter 有 identifier ZIP
,则 parameter 的子元素也应该这样命名.如何在 Xml-Schema 中实现这一点?
The naming of the element DealerCode is dependent to the identifier of the parameter element. For example if the parameter has the identifier ZIP
, the sub-element of parameter should also be named that way. How can I achieve this in the Xml-Schema?
例子:
<parameter type="string" isVisible="True" optional="False" id="DealerCode">
<DealerCode>ABCDEF001</DealerCode>
</parameter>
<parameter type="string" isVisible="True" optional="False" id="ZIP">
<ZIP>ABCDEF001</ZIP>
</parameter>
推荐答案
XSD 1.0
不可能.选项:
- 使用
xs:any
并针对 XSD 进行带外检查. 通过将通用
parameter
替换为它所包装的特定元素来重新设计您的 XML:
- Use
xs:any
and check out-of-band with respect to XSD. Redesign your XML by replacing the generic
parameter
with the specific element it wraps:
<DealerCode type="string" isVisible="True"
optional="False">ABCDEF001</DealerCode>
XSD 1.1
可能使用 xs:assert
:
<xs:assert test="*/local-name() = @id"/>
这是在您的完整 XSD 的上下文中,它将成功验证您的 XML:
Here it is in context of your complete XSD, which will successfully validate your XML:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:element name="DealerCode" type="xs:string"/>
<xs:element name="parameter">
<xs:complexType>
<xs:sequence>
<xs:any/>
</xs:sequence>
<xs:attribute type="xs:string" name="type"/>
<xs:attribute type="xs:string" name="isVisible"/>
<xs:attribute type="xs:string" name="optional"/>
<xs:attribute type="xs:string" name="id"/>
<xs:assert test="*/local-name() = @id"/>
</xs:complexType>
</xs:element>
<xs:element name="parameters">
<xs:complexType>
<xs:sequence>
<xs:element ref="parameter" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这篇关于Xml Schema中XML元素的依赖命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!