Skatteetaten / saf-t

Used for publishing of Norwegian SAF-T Schemas, codelists, testfiles etc. See also http://www.skatteetaten.no/saf-t for further documentation and information.
http://www.skatteetaten.no/saf-t
72 stars 111 forks source link

XML Schema constraint verification #14

Open misuo opened 7 years ago

misuo commented 7 years ago

Hi,

In the Norwegian_SAF-T_Financial_Schema_v_1.00.xsd XML Schema file a number of constraints are defined as e.g.

<?xml version="1.0"?>
<xs:schema xmlns="urn:StandardAuditFile-Taxation-Financial:NO"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:doc="urn:schemas-OECD:schema-extensions:documentation"
           targetNamespace="urn:StandardAuditFile-Taxation-Financial:NO"
           ...>
  ...
  <xs:element name="AuditFile">
    ...
    <xs:key name="KeyGeneralLedgerAccount">
      <xs:selector xpath="MasterFiles/GeneralLedgerAccounts/Account"/>
      <xs:field xpath="AccountID"/>
    </xs:key>
    ...
    <xs:keyref name="RefTransactionLineAccount" refer="KeyGeneralLedgerAccount">
      <xs:selector xpath="GeneralLedgerEntries/Journal/Transaction/Line"/>
      <xs:field xpath="AccountID"/>
    </xs:keyref>
    ...
  </xs:element>

  ...
</xs:schema>

The intention with the illustrated <xs:key> / <xs:keyref> pair of elements is that any account (AccountID) referenced within the transactions lines must reference a matching account within the master accounts. Otherwise it is a dangling/invalid account reference and the SAF-T xml file is to be considered invalid.

Many XML editors are capable - as long as both the .xml file and the .xsd file is provided - to validate and report such constraint validations. I've been using the Visual Studio editor, but my guess is that this is also possible with other editors, e.g. XMLSpy.

The problem is that I've tried in Visual Studio to validate an xml file that have an invalid account reference, i.e. a constraint validation, but it will unexpectedly not be reported by Visual Studio. So I've been investigating why that is.

I've found the reason and that has something to do with namespaces. Accordingly to Microsofts XML documentation on <xsd:selector> element the given xpath "... must use fully qualified names. For example, myNS:localName instead of localName.". It doesn't work with unqualified names, i.e. with no namespace. Whether this is specific to Microsoft or a general requirement I do not know.

I don't know whether other editors/validators exhibit the same problem. It could be worth checking.

It can be solved by updating the XML Schema .xsd file with a new "dummy" namespace - below I've used n1 - and then used that within the constraint specification elements, as illustrated below:

<?xml version="1.0"?>
<xs:schema xmlns="urn:StandardAuditFile-Taxation-Financial:NO"
           xmlns:n1="urn:StandardAuditFile-Taxation-Financial:NO"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:doc="urn:schemas-OECD:schema-extensions:documentation"
           targetNamespace="urn:StandardAuditFile-Taxation-Financial:NO"
           ...>
  ...
  <xs:element name="AuditFile">
    ...
    <xs:key name="KeyGeneralLedgerAccount">
      <xs:selector xpath="n1:MasterFiles/n1:GeneralLedgerAccounts/n1:Account"/>
      <xs:field xpath="n1:AccountID"/>
    </xs:key>
    ...
    <xs:keyref name="RefTransactionLineAccount" refer="KeyGeneralLedgerAccount">
      <xs:selector xpath="n1:GeneralLedgerEntries/n1:Journal/n1:Transaction/n1:Line"/>
      <xs:field xpath="n1:AccountID"/>
    </xs:keyref>
    ...
  </xs:element>
  ...
</xs:schema>

Then Visual Studio's Error List will report - here with an example - where I've updated the sample SAF-T file ExampleFile SAF-T Financial_999999999_20161125213512.xml at line 328 to refer to an 4001 AccountID (which is not within the master table):

image

I'm writing this to notify the XML Schema authors of Norwegian_SAF-T_Financial_Schema_v_1.00.xsd that at least one validator (Visual Studio and likely anything using Microsoft MSXML component) fails to catch the constraint validations without an update to the XML Schema. Potentially the same problem may occur for others as well? Is this something you've checked?

Try updating the AccountID at line 328 in the ExampleFile SAF-T Financial_999999999_20161125213512.xml to e.g. 4001 and reload the xml file in your preferred XML editor and verify whether it reports the constraint validation or not. Note that usually both the .xml and .xsd file should be present in same folder.