Schematron / schematron

Schematron "skeleton" - XSLT implementation
MIT License
93 stars 45 forks source link

Variables don't work in match #84

Open itmachines opened 5 years ago

itmachines commented 5 years ago

I have been battling for a while now with a schematron rule that does not seem to evaluate my variable correctly leaving me with no correct match.

My example and rule should always fail the assertion, and write out the message (as a "Test") ... as I want one question for every one reference (and visa versa) ... but the count always comes up as ZERO!

Where my rule is as follows:

<pattern id="Question">
  <rule id="Count" context="question/@id">
    <let name="href" value="concat('#', ancestor::parent/@id, '/', .)" />
    <assert test="false()">
      question/@id=[<value-of select="." />] has [<value-of select="count(ancestor::parent//reference[@href eq $href)])" />] references with reference/@href=[<value-of select="$href" />])
  </assert>
</pattern>

My input XML is as follows:

<parent id="GUID-ABCDEFG">
    <question>
        <references>
            <reference href="#GUID-ABCDEFG/GUID-12345"/>
            <reference href="#GUID-ABCDEFG/GUID-67890"/>
        </references>
        <questions>
            <question id="GUID-12345">...</question>
            <question id="GUID-67890">...</question>
        </questions>
    </question>
</parent>

My output when rendering outputs the variables correctly:

Question::Count:[[question/@id=[GUID-12345] has [0] references with reference/@href=[#GUID-ABCDEFG/GUID-12345]

As you can see, zero, when it should find 1 match.

If I count "all" the references, then it DOES give me 2!

Now, if I hard-code the count as a string: <value-of select="count(ancestor::parent//reference[@href eq '#GUID-ABCDEFG/GUID-12345'])" /> Then I do get the correct count/result!!

On another strange note: If I put a syntax error in my rule, the exception that is thrown shows me the "content" of the variable, not the value:

count(ancestor::question//data-about[@href eq concat('#', ancestor::question/@id, '/', .))])': syntax error, expecting ','

PStellmann commented 5 years ago

Hi Bradley,

after fixing the bugs in your schematron code it generated the expected output for me (Tested with oXygen xml 18.1):

<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2" xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
  <pattern id="Question">
    <rule id="Count" context="question/@id">
      <let name="href" value="concat('#', ancestor::parent/@id, '/', .)" />
      <assert test="false()">
        question/@id=[<value-of select="." />] has [<value-of select="count(ancestor::parent//reference[@href eq $href])" />] references with reference/@href=[<value-of select="$href" />])
      </assert>
    </rule>
  </pattern>
</schema>

Output:

question/@id=[GUID-12345] has [1] references with reference/@href=[#GUID-ABCDEFG/GUID-12345])
question/@id=[GUID-67890] has [1] references with reference/@href=[#GUID-ABCDEFG/GUID-67890])

About your exception: when the code is buggy there exists no value because the resulting script is not even being executed. Not sure what you expected here?

Regards, Patrik

itmachines commented 5 years ago

Hi Patrik, Thank-you for your reply and confirmation that it does work for you! It must be the version that I am using that does not work. I am using a .Net version (so also perhaps not the right area to have posted my question as well - so my apology for that!)

[assembly: AssemblyTitle("SchemaTron")] [assembly: AssemblyDescription("The native ISO Schematron validator over XPath 1.0 query language binding.")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("The XRouter Team")] [assembly: AssemblyProduct("SchemaTron")] [assembly: AssemblyCopyright("Copyright © The XRouter Team 2011")]

If I update my schema as you have it with the correct bindings, I get an error within the API: {The value of the 'queryBinding' attribute ('xslt2') is not valid.}

Can you please, point me in a direction for a .Net API version of Schematron that is functional, so that I may replace the one that I have perhaps?

Again, thanks for your help and confirmation!!!

All the best, Brad