Giter VIP home page Giter VIP logo

xsd2xml's Introduction

xsd2xml

This is a simple python script to help you generate some xmls if you have a xsd. It uses the xmlschema library to parse the given schema document and then populate some hardcoded values. check the following example.

XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root" type="root"/>
  <xs:complexType name="root">
    <xs:choice>
      <xs:sequence>
        <xs:element name="empno" type="xs:string"/>
        <xs:element name="designation" type="xs:string"/>
      </xs:sequence>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="age" type="xs:unsignedByte"/>
      </xs:sequence>
      <xs:element name="sku" type="SKU"/>
      <xs:element name="ppk" type="xs:integer"/>
      <xs:element name="alo" type="xs:date"/>
      <xs:any minOccurs="0"/>
      <xs:element name="shoesize">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:integer">
              <xs:attribute name="country" type="xs:string"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
      <xs:element name="jeans_size">
        <xs:simpleType>
          <xs:union memberTypes="sizebyno sizebystring"/>
        </xs:simpleType>
      </xs:element>
      <xs:element name="intvalues" type="valuelist"/>
    </xs:choice>
  </xs:complexType>
  <xs:simpleType name="SKU">
    <xs:restriction base="xs:string">
      <xs:pattern value="\d{3}\w{3}"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="sizebyno">
    <xs:restriction base="xs:positiveInteger">
      <xs:maxInclusive value="42"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="sizebystring">
    <xs:restriction base="xs:string">
      <xs:enumeration value="small"/>
      <xs:enumeration value="medium"/>
      <xs:enumeration value="large"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="valuelist">
    <xs:list itemType="xs:integer"/>
  </xs:simpleType>
</xs:schema>

XML

<?xml version="1.0" encoding="UTF-8"?>
<!--complex content-->
<root xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!--START:[choice]-->
  <!--next 9 items are in a [choice] group-->
  <!--START:[sequence]-->
  <!--next 2 items are in a [sequence] group-->
  <empno>lol</empno>
  <designation>lol</designation>
  <!--END:[sequence]-->
  <!--START:[sequence]-->
  <!--next 2 items are in a [sequence] group-->
  <name>lol</name>
  <age>17</age>
  <!--END:[sequence]-->
  <sku>lol</sku>
  <ppk>-176</ppk>
  <alo>2004-04-12</alo>
  <!--next 1 item is optional (minOcuurs = 0)-->
  <_ANY_/>
  <!--simple content-->
  <shoesize>-176</shoesize>
  <!--simpletype: union.-->
  <!--default: using the 1st type-->
  <jeans_size>+3</jeans_size>
  <!--simpletype: list-->
  <intvalues>-176</intvalues>
  <!--END:[choice]-->
</root>

The command to get the above xml:

$ python xsd2xml.py -s 1.xsd -e root | xmllint --format -

Usage

$ ./xsd2xml.py -h
usage: adv.xsd2xml.py [-h] -s XSDFILE -e ELEMENT [-c]

optional arguments:
  -h, --help            show this help message and exit
  -s XSDFILE, --schema XSDFILE
                        select the xsd used to generate xml
  -e ELEMENT, --element ELEMENT
                        select an element to dump xml
  -c, --choice          enable the <choice> mode

Handle <choice>

You can generate all in a choice group (as the above example shows) or you can generate the 1st element in the choice group by the "-c" option.

command

$ python xsd2xml.py -s 1.xsd -e root -c | xmllint --format -

xml

<?xml version="1.0" encoding="UTF-8"?>
<!--complex content-->
<root xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!--START:[choice]-->
  <!--next item is from a [choice] group with size=9-->
  <!--START:[sequence]-->
  <!--next 2 items are in a [sequence] group-->
  <empno>lol</empno>
  <designation>lol</designation>
  <!--END:[sequence]-->
  <!--END:[choice]-->
</root>

xsd2xml's People

Contributors

miaozn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xsd2xml's Issues

Attributes from main XSD not saved to XML and minor issue with remove_ns()

Hello, I have started using your xsd2xml tool (special thanks for your work), more specifically the updated branch from @seregagl, and I have encountered two main issues (being the second the one the most important, for which I have yet to find a fix). Since I am completely new to XSD syntax, they may sound simple and banal, for which I apologize.

  1. Function remove_ns() being passed "None" as "name". This causes the function to crash, trying to index None[0]. The following XSD attribute was the one causing the issue, although I am unsure why, since it was correctly validated by other XSD-checkers.
<xs:attribute name="match_edge">
      <xs:simpleType>
          <xs:restriction base="xs:string">
              <xs:enumeration value="full">
              </xs:enumeration>
              <xs:enumeration value="start">
              </xs:enumeration>
              <xs:enumeration value="end">
              </xs:enumeration>
          </xs:restriction>
      </xs:simpleType>
 </xs:attribute>
  1. Attributes that are defined within the main XSD are not saved to the output XML file. In this case some of the attributes from the XSD (first code block) were completely omitted in the output XML (second code block). Once again I am unsure why, and it was previously validated.
<xs:element name="FILES">
    <xs:complexType>
        <xs:sequence maxOccurs="1" minOccurs="1">
            <xs:element name="FILE" maxOccurs="unbounded">
              <xs:complexType>
                  <xs:attribute name="filename" type="xs:string" use="required">
                  </xs:attribute>
      </xs:complexType>
</xs:element>
<FILES>
      <FILE>
</FILE>
</FILES>

Other attributes from were correctly saved to the output XML, although in this case they were defined in a common space, instead of being directly specified within the said main XSD.

<xs:complexType name="NameType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="label" use="optional" type="xs:string"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>
<TAG label="String">String</PRIMARY_ID>

Please add requirements.txt or setup.py

Hello, Zach!

I tried to use your tool and it seems to work fine. One problem that I had was detecting which version of xmlschema I need to get installed. At the moment it's

xmlschema==0.9.14

Can you please add it to requirements.txt or to a setup.py?

which python version do you support?

With 3.7 I get:

~/.software/xsd2xml> ./xsd2xml.py 
Traceback (most recent call last):
  File "./xsd2xml.py", line 4, in <module>
    import xmlschema
ImportError: No module named xmlschema

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.