简体   繁体   English

xinclude + Intellij 的“无效 ID 引用”

[英]“Invalid id reference” for xinclude + Intellij

In the following example, test.xml shows no IntelliJ problem but test2.xml shows an Invalid id reference highlighting fooid red.在以下示例中, test.xml显示没有 IntelliJ 问题,但test2.xml显示无效的 id 引用,突出显示fooid红色。 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'.令人惊讶的是,这与test3.xml发生的情况不同,其中root元素也用IDREF 'foooid2' 没有 ID/IDREF 绑定进行注释。

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测试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>

test2.xml测试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>

test3.xml测试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>

I am wondering why the ID from test1 is not available in test2.我想知道为什么 test1 中的 ID 在 test2 中不可用。 Possibly this is an XY problem: maybe I am misusing XML/XSD and should solve my problem differently?可能这是一个 XY 问题:也许我误用了 XML/XSD,应该以不同的方式解决我的问题? What would be the best practice here?这里的最佳做法是什么?

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 eg some parts are imported multiple times into different other XML files (eg some basic part that is imported in 10 or so other xml files that then have nothing to do with each other).我的目标基本上是在单独的 XML 文件中定义更大的 XML 文件的部分,并包含它们以避免代码重复,例如,如果某些部分被多次导入到不同的其他 XML 文件中(例如,某些基本部分以 10 个左右的时间导入) xml 文件,然后彼此无关)。 Parsing those XML files also currently works but I am not happy with the red highlighting.解析这些 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.您需要将 xpointer 属性添加到包含,以仅添加指定的元素,否则包含将添加整个 xml 文件,这将导致架构验证错误。

<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"/>

Then your xml schema will fail because the "xml:base" attribute is not declared there.然后您的 xml 架构将失败,因为那里没有声明“xml:base”属性。 if you import the required w3 namespace and add a ref attribute it will work.如果您导入所需的 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>

update:更新:

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.在您发布关于需要通过 xpointer 包含的大量键值对的评论之后,我更改了 xml 架构的布局,因此它将接受一个列表,这可以正常工作。

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测试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>

test2.xml测试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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM