Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction between XInclude and <?xml-model> PI #1021

Closed
koh6uawi opened this issue Aug 21, 2024 · 9 comments · Fixed by eclipse-lemminx/lemminx#1670
Closed

Interaction between XInclude and <?xml-model> PI #1021

koh6uawi opened this issue Aug 21, 2024 · 9 comments · Fixed by eclipse-lemminx/lemminx#1670
Labels
bug Something isn't working relaxng validation
Milestone

Comments

@koh6uawi
Copy link

I am using the VSCode XML extension for a project using XInclude and a RelaxNG schema.

I faced the same problem as in the 2nd question of #845, so I ended up using the following structure:

common.rnc

namespace xml = "http://www.w3.org/XML/1998/namespace"

base = attribute xml:base { text }

chapter = element chapter {
  element title { text }
  & base?
}

document = element document {
  chapter+
}

document.rnc

include "common.rnc"

start = document

chapter.rnc

include "common.rnc"

start = chapter

document.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="document.rnc"?>
<document xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="chapter1.xml"/>
  <xi:include href="chapter2.xml"/>
</document>

chapter{1,2}.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="chapter.rnc"?>
<chapter>
  <title>Chapter</title>
</chapter>

It works well when I only include one chapter XML file, but when I try to include both, the extension raises the following error:

document.xml: 1 of 1 problem
There is '1' error in 'chapter2.xml'.xml
chapter2.xml(3, 2): element "chapter" not allowed here

It also starts working again as soon as I remove the <?xml-model> processing instruction from one of the files.

Is it the expected behaviour?

@angelozerr
Copy link
Contributor

To be honnest with you, I don't know. RelaxNG validation is done with https://github.com/relaxng/jing-trang library.

I suggest that you report your issue at https://github.com/relaxng/jing-trang

@koh6uawi
Copy link
Author

Thank you for your reply. When I use jing manually (in CLI mode), no error is reported:

$ jing -c document.rnc document.xml
$ jing -c chapter.rnc chapter1.xml
$ jing -c chapter.rnc chapter2.xml

My guess is that the information from the <?xml-model> PI "leaks" from the included file and mess with the model of the parent file.

As I don't think jing is responsible for parsing these instructions (to the best of my knowledge, jing has no such feature), it must have something to do with the way the VSCode XML extension or LemMinX handles it (but I am not really sure how).

@angelozerr
Copy link
Contributor

Indeed you are right. Badly I have no time to investigate your issue, any contribution are welcome.

If you need some help, please ask me.

@koh6uawi
Copy link
Author

I'll try to look into the LemMinX code.

In the meantime, I found a temporary fix, by adding an xpointer="element(/1)" attribute to the <xi:include/> elements in document.xml. By doing so, only the <chapter> elements are included, and the <?xml-model> PIs are skipped so they don't "pollute" the LemMinX validation.

(Another fix would be to use file association.)

@angelozerr
Copy link
Contributor

I'll try to look into the LemMinX code.

It should be really cool! I will try to do my best to assist you.

@koh6uawi
Copy link
Author

koh6uawi commented Aug 21, 2024

I've looked at the LemMinX code. The fix is quite trivial.

As stated in the Section 3 of the "Associating Schemas with XML documents" Group Note, for a PI with the xml-model target to be considered a potential xml-model PI, it must be located "in the [children] property of a document information item and appears before the element information item of the document information item's [children] property".

Contrary to this specification, the code at https://github.com/eclipse/lemminx/blob/main/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xerces/xmlmodel/XMLModelHandler.java#L124 looks anywhere in the document.

To fix that, one simply needs to add a beforeDocumentElement attribute to the XMLModelHandler class, set it to true at the beginning of the startDocument method and then to false at the first element encountered (via startElement and emptyElement). One can then check in the if condition of the processingInstruction method that beforeDocumentElement is true.

However, I'm unwilling to sign the Eclipse Contributor Agreement and give out my real name. If anyone who has signed this agreement wishes to commit the bugfix, they're free to do so.

@angelozerr
Copy link
Contributor

Thanks @koh6uawi for your feedback!

@angelozerr
Copy link
Contributor

@koh6uawi I created a PR at eclipse-lemminx/lemminx#1670 by following your suggestion and write a test.

Let's see if CI build is working again.

@koh6uawi
Copy link
Author

Thank you very much!

@angelozerr angelozerr added this to the 0.27.2 milestone Aug 23, 2024
@angelozerr angelozerr added bug Something isn't working validation relaxng labels Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working relaxng validation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants