[英]“Invalid id reference” for xinclude + Intellij
highlighting fooid
red.在以下示例中, test.xml
显示没有 IntelliJ 问题,但test2.xml
显示突出显示fooid
红色。 令人惊讶的是,这与test3.xml
发生的情况不同,其中root
元素也用
主文件
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attributeGroup name="attributeGroup_foo">
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:attributeGroup>
<xs:complexType name="complexType_foo">
<xs:attributeGroup ref="attributeGroup_foo"/>
</xs:complexType>
<xs:attributeGroup name="attributeGroup_bar">
<xs:attribute name="idref" type="xs:IDREF" use="required"/>
</xs:attributeGroup>
<xs:complexType name="complexType_bar">
<xs:attributeGroup ref="attributeGroup_bar"/>
</xs:complexType>
<xs:complexType name="complexType_root">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="root" type="complexType_root"/>
<xs:element name="foo" type="complexType_foo"/>
<xs:element name="bar" type="complexType_bar"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:element name="root" type="complexType_root"/>
</xs:schema>
测试文件
<?xml version="1.0" encoding="utf-8"?>
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xsi:noNamespaceSchemaLocation="main.xsd">
<foo id="fooid"/>
<bar idref="fooid"/>
</root>
测试1.xml
<?xml version="1.0" encoding="utf-8"?>
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xsi:noNamespaceSchemaLocation="main.xsd">
<foo id="fooid"/>
</root>
测试2.xml
<?xml version="1.0" encoding="utf-8"?>
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xsi:noNamespaceSchemaLocation="main.xsd">
<xi:include href="test1.xml" parse="xml">
<xi:fallback/>
</xi:include>
<bar idref="fooid"/>
</root>
测试3.xml
<?xml version="1.0" encoding="utf-8"?>
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xsi:noNamespaceSchemaLocation="main.xsd">
<xi:include href="test1.xml" parse="xml">
<xi:fallback/>
</xi:include>
<bar idref="fooid2"/>
</root>
我想知道为什么 test1 中的 ID 在 test2 中不可用。 可能这是一个 XY 问题:也许我误用了 XML/XSD,应该以不同的方式解决我的问题? 这里的最佳做法是什么?
我的目标基本上是在单独的 XML 文件中定义更大的 XML 文件的部分,并包含它们以避免代码重复,例如,如果某些部分被多次导入到不同的其他 XML 文件中(例如,某些基本部分以 10 个左右的时间导入) xml 文件,然后彼此无关)。 解析这些 XML 文件目前也有效,但我对红色突出显示不满意。
您需要将 xpointer 属性添加到包含,以仅添加指定的元素,否则包含将添加整个 xml 文件,这将导致架构验证错误。
<xi:include href="test1.xml" parse="xml" xpointer="fooid">
<xi:fallback/>
</xi:include>
如果使用此构造,组合输出的结果如下所示:
<foo id="fooid" xml:base="test1.xml"/>
然后您的 xml 架构将失败,因为那里没有声明“xml:base”属性。 如果您导入所需的 w3 命名空间并添加 ref 属性,它将起作用。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xml="http://www.w3.org/XML/1998/namespace">
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/03/xml.xsd" />
<xs:attributeGroup name="attributeGroup_foo">
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute ref="xml:base"/>
</xs:attributeGroup>
更新:
在您发布关于需要通过 xpointer 包含的大量键值对的评论之后,我更改了 xml 架构的布局,因此它将接受一个列表,这可以正常工作。
主文件
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xml="http://www.w3.org/XML/1998/namespace">
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/03/xml.xsd"/>
<xs:attributeGroup name="attributeGroup_foo">
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute ref="xml:base"/>
</xs:attributeGroup>
<xs:attributeGroup name="attributeGroup_foolist">
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute ref="xml:base"/>
</xs:attributeGroup>
<xs:complexType name="complexType_foo">
<xs:attributeGroup ref="attributeGroup_foo"/>
</xs:complexType>
<xs:attributeGroup name="attributeGroup_bar">
<xs:attribute name="idref" type="xs:IDREF" use="required"/>
</xs:attributeGroup>
<xs:complexType name="complexType_bar">
<xs:attributeGroup ref="attributeGroup_bar"/>
</xs:complexType>
<xs:complexType name="complexType_foolist">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="foolist" type="complexType_foolist"/>
<xs:element name="foo" type="complexType_foo"/>
<xs:element name="bar" type="complexType_bar"/>
</xs:choice>
</xs:sequence>
<xs:attributeGroup ref="attributeGroup_foolist"/>
</xs:complexType>
<xs:complexType name="complexType_root">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="foolist" type="complexType_foolist"/>
<xs:element name="foo" type="complexType_foo"/>
<xs:element name="bar" type="complexType_bar"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:element name="root" type="complexType_root"/>
</xs:schema>
测试1.xml
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:noNamespaceSchemaLocation="main.xsd">
<foolist id="foolist">
<foo id="fooid"/>
<foo id="fooid1"/>
<foo id="fooid2"/>
</foolist>
</root>
测试2.xml
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:noNamespaceSchemaLocation="main.xsd">
<xi:include href="test1.xml" parse="xml" xpointer="foolist">
<xi:fallback/>
</xi:include>
<bar idref="fooid"/>
<bar idref="fooid2"/>
</root>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.