How to generate XSD from elements of XML

前端 未结 5 1550
故里飘歌
故里飘歌 2021-02-19 23:23

I have a XML input


  id
  string
  42
  

        
相关标签:
5条回答
  • 2021-02-20 00:00

    Try xmlbeans it has some tools one of them is ins2xsd you can find specifics here: http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html Good luck

    0 讨论(0)
  • 2021-02-20 00:01

    You could write an XSLT to do something like that. But the problem is, a single document alone is not enough information to generate a schema. Are any of those elements optional? Is there anything missing from that document, that might appear in other instances? How many of a particular element can there be? Do they have to be in that order? There are loads of things that can be expressed in a schema, that are not immediately obvious from one instance of a document that conforms to that schema.

    0 讨论(0)
  • 2021-02-20 00:08

    If all you want is an XSD so that the XML you gave conforms to it, you'd be much better off by crafting it yourself rather than using a tool.

    No one knows better than you the particularities of the schema, such as which valid values are there (for instance, is the <maxlength> element required? are true and false the only valid values for <required>?).

    If you really want to use a tool (I'd only advice using it if you haven't designed the XML and really can't get the real XSD - or if you designed it, double check the generated XSD), you could try Trang. It can infer an XSD Schema from a number of example XML's.

    You'll have to take into account that the XSD a tool can infer you might be incomplete or inaccurate if XML samples aren't representative enough.

    java -jar trang.jar sampleXML.xml inferredXSD.xsd
    

    You can find a usage example of Trang here.

    0 讨论(0)
  • 2021-02-20 00:14

    You can try with online tool called XMLGrid: http://xmlgrid.net/xml2xsd.html

    0 讨论(0)
  • 2021-02-20 00:25

    For the people who really want to include it in their Java code to generate an XSD and understand the perils, check out Generate XSD from XML programatically in Java

    0 讨论(0)
提交回复
热议问题