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

PSRule - Failed to compile XPath expression in <rule>: '//*[. is nf:get-document-element(.)]' #77

Closed
wahowaho opened this issue May 30, 2019 · 14 comments
Assignees
Milestone

Comments

@wahowaho
Copy link

wahowaho commented May 30, 2019

hello,i have a question, content is: why Cannot find a 1-argument function named Q{http://reference.niem.gov/niem/specification/naming-and-design-rules/4.0/#NDRFunctions}get-document-element(). External function calls have been disabled,
schamatron is :

<sch:pattern id="rule_7-4"><sch:title>Document element is xs:schema</sch:title>
  <sch:rule context="*[. is nf:get-document-element(.)]">
    <sch:assert test="self::xs:schema">Rule 7-4: The [document element] of the [XML document] MUST have the name xs:schema.</sch:assert>
  </sch:rule>
</sch:pattern>

xml is :

<?xml version="1.0" encoding="UTF-8"?>
<xs:import schemaLocation="../../utility/structures/1.0/structures.xsd" namespace="http://release.ciem.org/ciem/structures/1.0/"/>
<xs:schema targetNamespace="http://release.ciem.org/ciem/sample/1.0/NegativeSample-4-1-1/" version="1.0"
           xsi:schemaLocation="http://release.ciem.org/ciem/sample/1.0/ ../../sample/1.0/NegativeSample-4-1-1.xsd"
           appinfo:defaultPrefix="NS-4-1-1" attributeFormDefault="qualified"
           blockDefault="" elementFormDefault="qualified"
           finalDefault="" xml:lang="/" xmlns:NS-4-1-1="http://release.ciem.org/ciem/sample/1.0/NegativeSample-4-1-1/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:hfp="http://www.w3.org/2001/XMLSchema-hasFacetAndProperty"
           xmlns:structures="http://release.ciem.org/ciem/structures/1.0/"
           xmlns:appinfo="http://release.ciem.org/ciem/appinfo/1.0/"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xs:annotation>
   <xs:documentation xml:lang="EN">The negative sample for the rule_7-4 in chapter 4.</xs:documentation>
  </xs:annotation>
</xs:schema>   

thanks

@phax phax self-assigned this May 30, 2019
@phax phax added the question label May 30, 2019
@phax
Copy link
Owner

phax commented May 30, 2019

Hi.
There is an error in the XML: "import" must be a child of "schema".
The topic of custom functions is tricky - you must supply them from the outside, or they must be part of the Schematron file itself.

  • Please paste the full Schematron (if possible)
  • What engine are you using? Must be "xslt" or "sch" to work?
  • How do you invoke it? Plain Java code? Maven plugin? ANT taks?

@wahowaho
Copy link
Author

wahowaho commented May 30, 2019

hello ! schematron is

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform" queryBinding="xslt2">
    <sch:title>NIEM example</sch:title>

    <xsl:include href="ndr-functions.xsl"/>

    <sch:ns prefix="xs" uri="http://www.w3.org/2001/XMLSchema"/>
    <sch:ns prefix="xsl" uri="http://www.w3.org/1999/XSL/Transform"/>
    <sch:ns prefix="nf" uri="http://reference.niem.gov/niem/specification/naming-and-design-rules/4.0/#NDRFunctions"/>
    <sch:ns prefix="ct" uri="http://release.niem.gov/niem/conformanceTargets/3.0/"/>
    <sch:ns prefix="xsi" uri="http://www.w3.org/2001/XMLSchema-instance"/>
    <sch:ns prefix="appinfo" uri="http://release.niem.gov/niem/appinfo/4.0/"/>
    <sch:ns prefix="structures" uri="http://release.niem.gov/niem/structures/4.0/"/>

    <sch:pattern id="rule_7-4"><sch:title>Document element is xs:schema</sch:title>
        <sch:rule context="*[. is nf:get-document-element(.)]">
            <sch:assert test="self::xs:schema">Rule 7-4: The [document element] of the [XML document] MUST have the name xs:schema.</sch:assert>
        </sch:rule>
    </sch:pattern>
</sch:schema> 

and i change my xml to :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://release.niem.org/niem/sample/1.0/NegativeSample-4-1-1/" version="1.0"
           blockDefault="" elementFormDefault="qualified"
           finalDefault=""
           xmlns:NS-4-1-1="http://release.niem.org/niem/sample/1.0/NegativeSample-4-1-1/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:hfp="http://www.w3.org/2001/XMLSchema-hasFacetAndProperty">
  <xs:annotation>
   <xs:documentation xml:lang="EN">The negative sample for the rule 4-1 in chapter 4.</xs:documentation>
  </xs:annotation>
</xs:schema>

thank you very much

@wahowaho
Copy link
Author

invoke it by Plain Java code final PSReader aReader = new PSReader(aRes);

@phax
Copy link
Owner

phax commented May 30, 2019

Ok thanks. Schematron looks okay.
Try this code as basis for validation:

    final ISchematronResource aSV = SchematronResourceSCH.fromfromFile ("patth/to/your.sch");
    final Document aDoc = aSV.applySchematronValidation (new FileSystemResource ("path/to/your.xml"));
   System.out.println (com.helger.xml.serialize.write.XMLWriter.getNodeAsString (aDoc));

Please ensure that the included XSL resides in the same folder as the SCH file

@wahowaho
Copy link
Author

hello!I'm sorry to bother you. i have try my best to do it,but it doesnt work. i cant find the reason. I wish to ask a favour of you. i push my project to github, I hope to get your help. ,I'm sorry to take up your time. think you very much ,thank you https://github.com/SnowMakerDemo/schematronTest.git

@phax
Copy link
Owner

phax commented May 30, 2019

It is indeed tricky - never used Schematron with XSLT includes.... Will play around with it and let you know :)

@wahowaho
Copy link
Author

OK thank you ,very thanks , i will change the content to xpath ,thank you :)

@wahowaho
Copy link
Author

hello i have another question。:)
exists(@appinfo:externalAdapterTypeIndicator) can not find an atrribute by "appinfo:externalAdapterTypeIndicator" , @appinfo:externalAdapterTypeIndicator != appinfo:externalAdapterTypeIndicator = "123"
but after remove ":" , Everything is running well.
thank you。

schematron is :
<sch:pattern id="rule_10-10">sch:titletest</sch:title>
<sch:rule context="xs:complexType[exists(@appinfo:externalAdapterTypeIndicator)]">
<sch:assert test="@appinfo:externalAdapterTypeIndicator ='123'">Rule 10-10: test.</sch:assert>
</sch:rule>
</sch:pattern>

xml is :
<xs:complexType id="123" name="annotationNegativeType" appinfo:externalAdapterTypeIndicator = "123">

</xs:complexType>

@phax
Copy link
Owner

phax commented May 31, 2019

I am out of office today. You need to declare the appinfo NS prefix explicitly. Attributes with namespaces are tricky in XPath...

@wahowaho
Copy link
Author

wahowaho commented Jun 3, 2019

thank you,I'm particularly grateful to you for having taken time out of your busy schedules to reply me. the holiday,i got merried,I didn't reply to you in time. that question is my mistake, sch import uri is different from xml import , i change it, now it‘s ok thank you

@phax
Copy link
Owner

phax commented Jun 3, 2019

Ah okay - congratulations on your marriage 😄
So you made it work? Can you please post your code here, because I also tried "sch:import" but I failed.
Thank you.

@wahowaho
Copy link
Author

wahowaho commented Jun 3, 2019

thank you,:) schematron : <sch:ns prefix="appinfo" uri="http://release.niem.gov/niem/appinfo/4.0/"/>
xml: xmlns:appinfo="http://release.nime.org/ciem/appinfo/1.0/" ,they are different from each other, when schematron check xml, schematron can not find appinfo exactly. its my second question what i said. but i think that is not you want what the question of sch:import . i did not modify the java code。im sorry,i let you down。

phax added a commit that referenced this issue Jun 11, 2019
@phax
Copy link
Owner

phax commented Jun 11, 2019

I think I found the error and fixed it in 5.1.0. The problem was a missing URLResolver for the created XSLT. I added a test for it and additionally improved the automatic URL resolver base URL determination. See https://github.com/phax/ph-schematron/blob/master/ph-schematron/src/test/resources/issues/github77/schematron.sch as the Schematron and https://github.com/phax/ph-schematron/blob/master/ph-schematron/src/test/java/com/helger/schematron/supplementary/Issue77Test.java for the respective test.
So

  1. use class SchematronResourceSCH and
  2. allow foreign element via aSCH.setAllowForeignElements (true); (for security reasons I cannot make this the default).

I will create the 5.1.0 release soon

@phax phax added the bug label Jun 11, 2019
@phax phax added this to the v5.1 milestone Jun 11, 2019
@phax phax closed this as completed Jun 11, 2019
@wahowaho
Copy link
Author

When I saw it, I was really happy, it solved my problem,thank you very much. 👍 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants