I'm trying to programatically update an an existing XSD in java that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="com/company/common" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="com/company/common/" elementFormDefault="qualified">
<xs:include schemaLocation="DerivedAttributes.xsd" />
<xs:element name="MyXSD" type="MyXSD" />
<xs:complexType name="Container1">
<xs:sequence>
<xs:element name="element1" type="element1" minOccurs="0"
maxOccurs="unbounded" />
<xs:element name="element2" type="element2" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Container2">
<xs:sequence>
<xs:element name="element3" type="Type1" minOccurs="0" />
<xs:element name="element4" type="Type2" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I am able to add a new element to Container 1 very easily with DOM and XPath like this:
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(
new InputSource("test.xsd"));
// use xpath to find node to add to
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xPath
.evaluate(
"/schema/complexType[@name=\"Container1\"]/sequence",
doc.getDocumentElement(), XPathConstants.NODESET);
// create element to add
org.w3c.dom.Element newElement = doc.createElement("xs:element");
newElement.setAttribute("name", "element5");
newElement.setAttribute("type", "type5");
newElement.setAttribute("minOccurs", "0");
newElement.setAttribute("manOccurs", "unbounded");
nodes.item(0).appendChild(newElement);
// output
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource(doc.getDocumentElement()),
new StreamResult(System.out));
And I am able to get this result:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="com/company/common" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="com/company/common/" elementFormDefault="qualified">
<xs:include schemaLocation="DerivedAttributes.xsd" />
<xs:element name="MyXSD" type="MyXSD" />
<xs:complexType name="Container1">
<xs:sequence>
<xs:element name="element1" type="element1" minOccurs="0"
maxOccurs="unbounded" />
<xs:element name="element2" type="element2" minOccurs="0"
maxOccurs="unbounded" />
<xs:element name="element3" type="element2" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Container2">
<xs:sequence>
<xs:element name="element3" type="Type1" minOccurs="0" />
<xs:element name="element2" type="Type2" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Container3">
<xs:sequence>
<xs:element name="element4" type="Type1" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>
So my question is how can I add a new complex type named "Container 3"...with a sequence...that contains "element 5" using the same DOM apprach so it looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="com/company/common" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="com/company/common/" elementFormDefault="qualified">
<xs:include schemaLocation="DerivedAttributes.xsd" />
<xs:element name="MyXSD" type="MyXSD" />
<xs:complexType name="Container1">
<xs:sequence>
<xs:element name="element1" type="element1" minOccurs="0"
maxOccurs="unbounded" />
<xs:element name="element2" type="element2" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Container2">
<xs:sequence>
<xs:element name="element3" type="Type1" minOccurs="0" />
<xs:element name="element4" type="Type2" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Container3">
<xs:sequence>
<xs:element name="element5" type="Type1" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Right now im using a DOM parser that adds a new complex type...But im not sure how to create a complex type that also has sequence with an element. This is what I have so far...
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(
new InputSource("test.xsd"));
// use xpath to find node to add to
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xPath.evaluate("/schema", doc
.getDocumentElement(), XPathConstants.NODESET);
// create element to add
org.w3c.dom.Element newElement = doc.createElement("xs:complexType");
newElement.setAttribute("name", "Container3");
nodes.item(0).appendChild(newElement);
// output
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource(doc.getDocumentElement()),
new StreamResult(System.out));
Thanks!