开发者

Getting the XSD source from an org.eclipse.xsd.XSDSchema object?

开发者 https://www.devze.com 2023-03-17 18:11 出处:网络
I\'m parsing an XML Schema file (*.xsd) using org.eclipse.xsd.XSDSchema and would like to display some XSDTypeDefinitions as literal XSD source.

I'm parsing an XML Schema file (*.xsd) using org.eclipse.xsd.XSDSchema and would like to display some XSDTypeDefinitions as literal XSD source.

Here's an example schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="shiporder">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="orderperson" type="xs:string"/>
          <xs:element name="shipto">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="address" type="xs:string"/>
                <xs:element name="city" type="xs:string"/>
                <xs:element name="country" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
      </xs:complexType>
    </xs:element>
</xs:schema>

And here's some code for illustrating the problem:

XSDSchema schema = loadSchemaFromFile(); // not shown
for(XSDElementDeclaration element : schema.getElementDeclarations()){
    assert element.getName().equals("shiporder");

    String xsdSource = // NOW WHAT DO I NEED TO DO HERE?

    String expectedXsdSource = "<xs:element name=\"shiporder\">" +
        "  <xs:complexType>" +
        "    <xs:sequence>" +
        "      <xs:element name=\"orderperson\" type=\开发者_StackOverflow社区"xs:string\"/>" +
        "      <xs:element name=\"shipto\">" +
        "        <xs:complexType>" +
        "          <xs:sequence>" +
        "            <xs:element name=\"address\" type=\"xs:string\"/>" +
        "          </xs:sequence>" +
        "        </xs:complexType>" +
        "      </xs:element>" +
        "  </xs:complexType>" +
        "</xs:element>";

    assert xsdSource.equals(expectedXsdSource);
}

I'm using the following jar files from an Eclipse 3.7 installation:

  • org.eclipse.xsd_2.7.0.v20110606-0949.jar
  • org.eclipse.emf.ecore_2.7.0.v20110605-0747.jar
  • org.eclipse.emf.common_2.7.0.v20110605-0747.jar


Comparing two XML documents using String.equals might be hazardous as white space might differ even though it ought not affect equality.

How about using isEqualNode? You could convert org.eclipse.xsd.XSDSchema to org.w3c.dom.Document by using the getDocument method and the following guide to convert your schema string?

0

精彩评论

暂无评论...
验证码 换一张
取 消