“无效的 ID 引用"对于 xinclude + Intellij [英] "Invalid id reference" for xinclude + Intellij
问题描述
在下面的例子中,test.xml
显示没有 IntelliJ 问题,但 test2.xml
显示一个 Invalid id reference 突出显示 fooid
红色.令人惊讶的是,这与 test3.xml
发生的情况不同,其中 root
元素也用 注释,IDREF 'foooid2' 没有 ID/IDREF 绑定.
In the following example, test.xml
shows no IntelliJ problem but test2.xml
shows an Invalid id reference highlighting fooid
red.
Surprisingly, this is different to what happens with test3.xml
where also the root
elements are annotated with There is no ID/IDREF binding for IDREF 'fooid2'.
main.xsd
<?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>
test.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"/>
<bar idref="fooid"/>
</root>
test1.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>
test2.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>
test3.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,应该以不同的方式解决我的问题?这里的最佳做法是什么?
I am wondering why the ID from test1 is not available in test2. Possibly this is an XY problem: maybe I am misusing XML/XSD and should solve my problem differently? What would be the best practice here?
我的目标基本上是在单独的 XML 文件中定义更大的 XML 文件的各个部分,并包含它们以避免代码重复,例如有些部分被多次导入到不同的其他 XML 文件中(例如,一些基本部分在 10 个左右的其他 xml 文件中导入,然后彼此无关).解析这些 XML 文件目前也有效,但我对红色突出显示不满意.
My goal is basically to define parts of a bigger XML file in separate XML files and to include them to avoid code duplicates if e.g. some parts are imported multiple times into different other XML files (e.g. some basic part that is imported in 10 or so other xml files that then have nothing to do with each other). Parsing those XML files also currently works but I am not happy with the red highlighting.
推荐答案
你需要在 include 添加 xpointer 属性,只添加指定的元素,否则 include 会添加整个 xml 文件,这将导致架构验证错误.
You need to add the xpointer attribute to the include, to only add the specified element, else the include will add the whole xml file which will cause a schema validation error.
<xi:include href="test1.xml" parse="xml" xpointer="fooid">
<xi:fallback/>
</xi:include>
如果您使用此构造,组合输出的结果如下所示:
If you use this construct the result of the combined output looks like this:
<foo id="fooid" xml:base="test1.xml"/>
然后您的 xml 模式将失败,因为xml:base"属性未在那里声明.如果您导入所需的 w3 命名空间并添加 ref 属性,它将起作用.
Then your xml schema will fail because the "xml:base" attribute is not declared there. if you import the required w3 namespace and add a ref attribute it will work.
<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 架构的布局,以便它接受一个列表,这可以正常工作.
after the comment you posted regarding having a lot of key value pairs that need to be included via xpointer I changed the layout of your xml schema so it will accept a list and this works fine.
main.xsd
<?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>
test1.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>
test2.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>
这篇关于“无效的 ID 引用"对于 xinclude + Intellij的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!