fnogatz / xsd2json

Translate XML Schema into equivalent JSON Schema
MIT License
149 stars 28 forks source link

Prolog initialisation failed #54

Open lmichel opened 7 years ago

lmichel commented 7 years ago

I'm sorry to reopen an issue which is rather likely a user support request (I'm an absolute beginner with Prolog). I guess that some other users can have to face the same problem.

I properly setup my swipl with Aptitude and swivm as suggested in thread #53

michel@obs-he-lm:~$ swipl --version
SWI-Prolog  version 7.2.3 for x86_64-linux
michel@obs-he-lm:~$ swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.2.3)
Copyright (c) 1990-2015 ....
?- use_module(library(sgml)).
true.
?- use_module(library(chr)).
true.

When I try to process a schema, I get nothing on stdout; then I switch to the trace mode and I get the following output (truncated).

michel@obs-he-lm:~$ xsd2json -t purchaseorder.xsd
CHR:   (0) Insert: node_attribute(/home/michel/purchaseorder.xsd,[0],xmlns:xsd,http://www.w3.org/2001/XMLSchema,source) # <0>
....
....
CHR:   (1) Redo: node_attribute(/home/michel/purchaseorder.xsd,[0],xmlns:xsd,http://www.w3.org/2001/XMLSchema,source) # <0>
CHR:   (0) Fail: node_attribute(/home/michel/purchaseorder.xsd,[0],xmlns:xsd,http://www.w3.org/2001/XMLSchema,source) # <0>
ERROR: Prolog initialisation failed:
ERROR: Domain error: `chr_port' expected, found `none'

I've no idea at all (and Google either) about that chr_port. Is there some missing parameter or resource?

LM

fnogatz commented 7 years ago

Could you please link to the used purchaseorder.xsd file or send it to (deleted), so I can have a look at it?

lmichel commented 7 years ago
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation xml:lang="en">
     Purchase order schema for Example.com.
     Copyright 2000 Example.com. All rights reserved.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>

  <xsd:element name="comment" type="xsd:string"/>

  <xsd:complexType name="PurchaseOrderType">
    <xsd:sequence>
      <xsd:element name="shipTo" type="USAddress"/>
      <xsd:element name="billTo" type="USAddress"/>
      <xsd:element ref="comment" minOccurs="0"/>
      <xsd:element name="items"  type="Items"/>
    </xsd:sequence>
    <xsd:attribute name="orderDate" type="xsd:date"/>
  </xsd:complexType>

  <xsd:complexType name="USAddress">
    <xsd:sequence>
      <xsd:element name="name"   type="xsd:string"/>
      <xsd:element name="street" type="xsd:string"/>
      <xsd:element name="city"   type="xsd:string"/>
      <xsd:element name="state"  type="xsd:string"/>
      <xsd:element name="zip"    type="xsd:decimal"/>
    </xsd:sequence>
    <xsd:attribute name="country" type="xsd:NMTOKEN"
                   fixed="US"/>
  </xsd:complexType>

  <xsd:complexType name="Items">
    <xsd:sequence>
      <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="productName" type="xsd:string"/>
            <xsd:element name="quantity">
              <xsd:simpleType>
                <xsd:restriction base="xsd:positiveInteger">
                  <xsd:maxExclusive value="100"/>
                </xsd:restriction>
              </xsd:simpleType>
            </xsd:element>
            <xsd:element name="USPrice"  type="xsd:decimal"/>
            <xsd:element ref="comment"   minOccurs="0"/>
            <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
          </xsd:sequence>
          <xsd:attribute name="partNum" type="SKU" use="required"/>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>

  <!-- Stock Keeping Unit, a code for identifying products -->
  <xsd:simpleType name="SKU">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="\d{3}-[A-Z]{2}"/>
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>
fnogatz commented 7 years ago

The bug happened because of types starting with capital letters, in this case XSD's built-in NMTOKEN, which was handled incorrectly and therefore recognized as a Prolog variable. I added three new tests and a fix, so the given purchaseorder.xsd example results in a valid JSON Schema representation. Unfortunately I have to take a further look at the other example file you sent me via mail as it still fails.

fnogatz commented 7 years ago

This is... strange. Unfortunately SWI-Prolog is not good at versioning (semver would be too easy, right?), but according to my information split_string/4 should be part of SWI-Prolog 7. I even tried with a local installation of v7.2.3 like you are using. Could you please open a SWI-Prolog REPL (simply call swipl from command line) and run the following query:

split_string('a:B',':','', L).

Does this work? What's the output?

edit: Replaced split_string/4 by atomic_list_concat/3 again for SWI6 portability.

lmichel commented 7 years ago

Sorry, I ran a wrong swipl version (6.xx). The problem is that the installed xsd2json does not take into account the swivm predicat (use 7.2). That seems to work from the git clone:

michel@obs-he-lm:~/gitRepositories/xsd2json$ which swipl
/home/michel/.swivm/versions/7.2.3/bin/swipl

But that fails from the CLI. If I, look at the xsd2json trace, I can see that the native install of swipl is invoked instead of the cloned one

michel@obs-he-lm:~/gitRepositories/xsd2json$ which swipl
/home/michel/.swivm/versions/7.2.3/bin/swipl
michel@obs-he-lm:~/gitRepositories/xsd2json$ strace -f xsd2json -t purchaseorder.xsd 2>&1 | grep swipl 
execve("/usr/lib/swi-prolog/bin/amd64/swipl", ["/usr/lib/swi-prolog/bin/amd64/sw"..., "-x", "/usr/bin/xsd2json", "--", "-t", "purch
aseorder.xsd"], [/* 70 vars */]) = 0
....

This is a shell issue.

fnogatz commented 7 years ago

Nice catch! I am not sure why the wrong swipl executable is used. Nevertheless I checked the Prolog predicates specific for SWI-Prolog 7 and replaced them by predicates which work in both versions. So xsd2json is compatible to SWI-Prolog 6 again (v1.7.14@npm). But this does yet not solve the problem for your second test file :)

lmichel commented 7 years ago

Falco,

I confirm : 1) v1.7.14@npm works with swipl 6.xx. 2) Purchaseorder.xsd passes 3) VOTable.xsd fails on ERROR: Prolog initialisation failed: ERROR: Domain error: chr_port' expected, foundnone'

LM

Le 03/01/2017 à 17:21, Falco Nogatz a écrit :

Nice catch! I am not sure why the wrong swipl executable is used. Nevertheless I checked the Prolog predicates specific for SWI-Prolog 7 and replaced them by predicates which work in both versions. So xsd2json is compatible to SWI-Prolog 6 again (v1.7.14@npm). But this does yet not solve the problem for your second test file :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fnogatz/xsd2json/issues/54#issuecomment-270153797, or mute the thread https://github.com/notifications/unsubscribe-auth/AK7n-NuQ8yi1KygA8FKMkemlz1u_hAW4ks5rOnWKgaJpZM4LZf_U.

-- jesuischarlie/Tunis/Paris/Bruxelles/Berlin

Laurent Michel SSC XMM-Newton Tél : +33 (0)3 68 85 24 37 Fax : +33 (0)3 )3 68 85 24 32 Université de Strasbourg http://www.unistra.fr Observatoire Astronomique 11 Rue de l'Université F - 67200 Strasbourg

fnogatz commented 7 years ago

Okay, had to rewrite quite some code but it is working now, even for your VOTable.xsd test file (just be patient - the translation takes about 12 seconds on my machine). The only thing that's missing is the conversion of the following type:

<xs:complexType name="anyTEXT" mixed="true">
  <xs:sequence>
    <xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
  </xs:sequence>
</xs:complexType>

I'm not sure this is the best way to specify an element that contains HTML in XML Schema. What would you expect to be a valid JSON Schema translation?

fnogatz commented 7 years ago

The updated version is available as 1.8.0 on npm.

lmichel commented 7 years ago

Thanks,

Le 03/01/2017 à 21:58, Falco Nogatz a écrit :

Okay, had to rewrite quite some code but it is working now, even for your |VOTable.xsd| test file (just be patient - the translation takes about 12 seconds on my machine). Never mind, It could take hours since this operation is meant to be rarely operated.

The only thing that's missing is the conversion of the following type:

| </xs:sequence> </xs:complexType> |

I'm not sure this is the best way to specify an element that contains HTML in XML Schema. What would you expect to be a valid JSON Schema translation? This schema results from very long discussions to support an incredible number of use cases. So I couldn't say why this statement is here, but it cannot be changed for sure. AS far I understand, the problem comes from sequence of . This element is supposed to contain some elements from another schema e.g. or an anything else. In our case this fragment is not processed. It must therefore be considered as a string. In term of JSON, that would mean that the element can contain a JSON string which must not be interpreted as an structured type. Something like: {"element" : "{key:value, key: value.....}"} Considering this, I would say that using a JSON String for the xs:any should work, at least for our use case.

The real test to me is now to check the schema against data files ... in progress To do this, I'm using http://www.jsonschemavalidator.net/ (standalone version not free)

LM

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fnogatz/xsd2json/issues/54#issuecomment-270222780, or mute the thread https://github.com/notifications/unsubscribe-auth/AK7n-J02dzIoFytL-xQNXTt5iITiDKkQks5rOrZtgaJpZM4LZf_U.

-- jesuischarlie/Tunis/Paris/Bruxelles/Berlin

Laurent Michel SSC XMM-Newton Tél : +33 (0)3 68 85 24 37 Fax : +33 (0)3 )3 68 85 24 32 Université de Strasbourg http://www.unistra.fr Observatoire Astronomique 11 Rue de l'Université F - 67200 Strasbourg

lmichel commented 7 years ago

In the generated JSON schema 2 complexTypes are actually missing:

1) #/definitions/anyTEXT as you said

2) #/definitions/Info : This definition is used within another definition (Data): might this cause trouble?

I replaces those types with Strings and started to create a basic and compliant data file: works fine right now. LM

Le 03/01/2017 à 21:58, Falco Nogatz a écrit :

Okay, had to rewrite quite some code but it is working now, even for your |VOTable.xsd| test file (just be patient - the translation takes about 12 seconds on my machine). The only thing that's missing is the conversion of the following type:

| </xs:sequence> </xs:complexType> |

I'm not sure this is the best way to specify an element that contains HTML in XML Schema. What would you expect to be a valid JSON Schema translation?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fnogatz/xsd2json/issues/54#issuecomment-270222780, or mute the thread https://github.com/notifications/unsubscribe-auth/AK7n-J02dzIoFytL-xQNXTt5iITiDKkQks5rOrZtgaJpZM4LZf_U.

-- jesuischarlie/Tunis/Paris/Bruxelles/Berlin

Laurent Michel SSC XMM-Newton Tél : +33 (0)3 68 85 24 37 Fax : +33 (0)3 )3 68 85 24 32 Université de Strasbourg http://www.unistra.fr Observatoire Astronomique 11 Rue de l'Université F - 67200 Strasbourg