charto / cxsd

Streaming XSD parser and XML parser generator with TypeScript output
MIT License
111 stars 55 forks source link

strange parsing error #8

Closed geoHeil closed 8 years ago

geoHeil commented 8 years ago

Trying to parse the following xsd (which validates fine) results in a strange error. --> the example of npm run cxsd http://schemas.opengis.net/wfs/1.1.0/wfs.xsd works just fine.

Here the xsd:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" vc:minVersion="1.1"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning">
  <xs:element name="myElem" type="myElem"/>
  <xs:complexType name="myElem">
    <xs:sequence>
      <xs:element name="geburtsdatum" type="xs:date" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

here the error:

verbose cli   'run',
1 verbose cli   'cxdd',
1 verbose cli   'vaas_kfz_berechnung.xsd' ]
2 info using npm@2.15.5
3 info using node@v4.4.5
4 verbose run-script [ 'precxsd', 'cxsd', 'postcxsd' ]
5 info precxsd @
6 info cxdd @
7 verbose unsafe-perm in lifecycle true
8 info @ Failed to exec cxdd script
9 verbose stack Error: @ cxsd: `cxdd "filename.xsd"`
9 verbose stack Exit status 1
9 verbose stack     at EventEmitter.<anonymous> (/Users/geoHeil/.nvm/versions/node/v4.4.5/lib/node_modules/npm/lib/utils/lifecycle.js:217:16)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at EventEmitter.emit (events.js:172:7)
9 verbose stack     at ChildProcess.<anonymous> (/Users/geoHeil/.nvm/versions/node/v4.4.5/lib/node_modules/npm/lib/utils/spawn.js:24:14)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at ChildProcess.emit (events.js:172:7)
9 verbose stack     at maybeClose (internal/child_process.js:827:16)
9 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
10 verbose pkgid @
11 verbose cwd /path/to/folder
12 error Darwin 15.5.0
13 error argv "/Users/geoHeil/.nvm/versions/node/v4.4.5/bin/node" "/Users/geoHeil/.nvm/versions/node/v4.4.5/bin/npm" "run" "cxsd" "filename.xsd"
14 error node v4.4.5
15 error nam  v2.15.5
16 error code LIFECYCLE
17 error @ cxsd: `cxdd "filename.xsd"`
17 error Exit status 1
jjrv commented 8 years ago

It seems you ran the command cxdd instead of cxsd.

geoHeil commented 8 years ago

Strange. But I definitely ran: npm run cxdd someAwesomeSchema.xsd

and just confirmed that the issue persists.

jjrv commented 8 years ago

You should instead run:

npm run cxsd someAwesomeSchema.xsd

jjrv commented 8 years ago

And if your package.json has a script cxdd it should also be called cxsd.

geoHeil commented 8 years ago

Sorry for the strange copy / paste error but in the terminal and now confirmed again it reads npm run cxsd someAwesomeSchema.xsd but when I format this as code the s is replaced with a d like npm run cxdd someAwesomeSchema.xsd

I did confirm that the package.json does not contain cxdd. The error still persists.

jjrv commented 8 years ago

npm gave a really strange error message there but the actual reason seems to be that cxsd wants a remote URL address as a parameter, so you could put your .xsd file online somewhere or try the following commands in the terminal (in the directory where you're running the npm commands):

sudo cp someAwesomeSchema.xsd /Library/WebServer/Documents/
sudo apachectl start
rm -rf cache && npm run cxsd http://localhost/someAwesomeSchema.xsd

That should set up a local web server providing the file. You can stop it with sudo apachectl stop afterwards. The rm -rf cache command is necessary to reload the .xsd file if you change it.

cxsd also wants the .xsd file to have a target namespace, so it could contain:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" vc:minVersion="1.1"
  targetNamespace="http://some/awesome/schema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning">
  <xsd:element name="myElem" type="myElem"/>
  <xsd:complexType name="myElem">
    <xsd:sequence>
      <xsd:element name="geburtsdatum" type="xsd:date" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

This worked for me and produced the following TypeScript definitions under xmlns/some/awesome/schema.d.ts:

import * as Primitive from '../../xml-primitives';

// Source files:
// http://localhost/someAwesomeSchema.xsd

interface BaseType {
    _exists: boolean;
    _namespace: string;
}
interface _myElem extends BaseType {
    geburtsdatum?: Date;
}
export interface myElem extends _myElem { constructor: { new(): myElem }; }
export var myElem: { new(): myElem };

export interface document extends BaseType {
    myElem: myElem;
}
export var document: document;
geoHeil commented 8 years ago

npm run cxdd https://gist.githubusercontent.com/geoHeil/865af42ddfeb2707150cb5ef05a7b3e5/raw/5adbca31cc3844e2184121580e19b8658d3e66dc/gistfile1.txt

leads to a: [TypeError: Cannot read property 'getScope' of undefined]

What did you do different?

jjrv commented 8 years ago

Add a targetNamespace="http://some/awesome/schema" attribute inside the opening <xs:schema> tag.

Later I'll probably fix the error message and add a command line parameter to specify the namespace if it's missing from the .xsd.

geoHeil commented 8 years ago

Thanks for the tip. Finally this works. for https://gist.githubusercontent.com/geoHeil/865af42ddfeb2707150cb5ef05a7b3e5/raw/abdd0ad0ac9f93450e4ef41a5b9d20e13dee2cea/someAwesomeSchema.xsd