Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » XML Schema Definition (XSD) » Problem during replacing element from one schema to another schema using DOM
Problem during replacing element from one schema to another schema using DOM [message #77668] Fri, 15 May 2009 04:29 Go to next message
Bhuvan Mehta is currently offline Bhuvan MehtaFriend
Messages: 58
Registered: July 2009
Member
Hello,

I am trying to replace an element from one schema(source) to another
schema(target) schema using DOM APIs as below :
and the replacement is not happening properly.

------------------------------------------------------------ --
TestContentCopy.java
------------------------------------------------------------ --
package test.schema;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceImpl;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class TestContentCopy implements IObjectActionDelegate {

public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}

public void run(IAction action) {

String sourceLoc = "C:\\test1.xsd";
String targetLoc = "C:\\test2.xsd";

File file1 = new File(sourceLoc);
File file2 = new File(targetLoc);

final ResourceSet resourceSet = new ResourceSetImpl();
final Resource res1 =
resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
final Resource res2 =
resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);

XSDSchema sourceSchema = ((XSDResourceImpl) res1).getSchema();
XSDSchema targetSchema = ((XSDResourceImpl) res2).getSchema();

XSDElementDeclaration srcComponent =
sourceSchema.getElementDeclarations().get(0);
XSDElementDeclaration oldComponent =
targetSchema.getElementDeclarations().get(0);

Element element = srcComponent.getElement();
if (element != null) {
final Node newComponent =
targetSchema.getDocument().importNode(srcComponent.getElemen t(), true);
targetSchema.getElement().replaceChild(newComponent,
oldComponent.getElement());
targetSchema.getDocument().normalizeDocument();
}

targetSchema.validate();
final Collection<XSDDiagnostic> diag = targetSchema.getAllDiagnostics();
if (diag != null && !diag.isEmpty()) {
for (final XSDDiagnostic dia : diag) {
System.out.println(dia.getMessage());
}
}
Resource resource = targetSchema.eResource();
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
}

public void selectionChanged(IAction action, ISelection selection) {
}

}
------------------------------------------------------------ --
Test1.xsd
------------------------------------------------------------ --
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/test1"
xmlns:tns1="http://www.example.org/test1" elementFormDefault="qualified">

<element name="ele1" type="tns1:simple1" nillable="true"/>
<simpleType name="simple1">
<union memberTypes="int">
<annotation>
<documentation>
SimpleType.
</documentation>
</annotation>
</union>
</simpleType>

</schema>
------------------------------------------------------------ --
Test2.xsd
------------------------------------------------------------ --
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/test1"
elementFormDefault="qualified"
targetNamespace="http://www.example.org/test1">

<element name="ele1" type="tns:simple1" />

<simpleType name="simple1">
<union memberTypes="int">
<annotation>
<documentation>
SimpleType.
</documentation>
</annotation>
</union>
</simpleType>

</schema>

------------------------------------------------------------ -------
Test2.xsd (After Code execution)
------------------------------------------------------------ -------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/test1" elementFormDefault="qualified"
targetNamespace="http://www.example.org/test1">

<element name="ele1" nillable="true" type="tns1:simple1"/>

<simpleType name="simple1">
<union memberTypes="int">
<annotation>
<documentation>
SimpleType.
</documentation>
</annotation>
</union>
</simpleType>

</schema>

------------------------------------------------------------ ------
Validation falis with below message
------------------------------------------------------------ ------
XSD: Type reference 'tns1#simple1' is unresolved, which is true as
replacechild() method should have defined "tns1" in element ?

Am i doing something wrong or is there any other way to do this.

--
Thanks and Regards,
Bhuvan Mehta
Re: Problem during replacing element from one schema to another schema using DOM [message #77689 is a reply to message #77668] Fri, 15 May 2009 19:50 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Bhuvan,

It's unlikely I'll have time to look at this before June 26th,
especially if I have to set up a project to even run this scenario.


Bhuvan wrote:
> Hello,
>
> I am trying to replace an element from one schema(source) to another
> schema(target) schema using DOM APIs as below :
> and the replacement is not happening properly.
>
> ------------------------------------------------------------ --
> TestContentCopy.java
> ------------------------------------------------------------ --
> package test.schema;
>
> import java.io.File;
> import java.io.IOException;
> import java.util.Collection;
> import java.util.Collections;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.jface.action.IAction;
> import org.eclipse.jface.viewers.ISelection;
> import org.eclipse.ui.IObjectActionDelegate;
> import org.eclipse.ui.IWorkbenchPart;
> import org.eclipse.xsd.XSDDiagnostic;
> import org.eclipse.xsd.XSDElementDeclaration;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.util.XSDResourceImpl;
> import org.w3c.dom.Element;
> import org.w3c.dom.Node;
>
> public class TestContentCopy implements IObjectActionDelegate {
>
> public void setActivePart(IAction action, IWorkbenchPart targetPart) {
> }
>
> public void run(IAction action) {
>
> String sourceLoc = "C:\\test1.xsd";
> String targetLoc = "C:\\test2.xsd";
>
> File file1 = new File(sourceLoc);
> File file2 = new File(targetLoc);
>
> final ResourceSet resourceSet = new ResourceSetImpl();
> final Resource res1 =
> resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
> final Resource res2 =
> resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);
>
> XSDSchema sourceSchema = ((XSDResourceImpl) res1).getSchema();
> XSDSchema targetSchema = ((XSDResourceImpl) res2).getSchema();
>
> XSDElementDeclaration srcComponent =
> sourceSchema.getElementDeclarations().get(0);
> XSDElementDeclaration oldComponent =
> targetSchema.getElementDeclarations().get(0);
>
> Element element = srcComponent.getElement();
> if (element != null) {
> final Node newComponent =
> targetSchema.getDocument().importNode(srcComponent.getElemen t(), true);
> targetSchema.getElement().replaceChild(newComponent,
> oldComponent.getElement());
> targetSchema.getDocument().normalizeDocument();
> }
>
> targetSchema.validate();
> final Collection<XSDDiagnostic> diag = targetSchema.getAllDiagnostics();
> if (diag != null && !diag.isEmpty()) {
> for (final XSDDiagnostic dia : diag) {
> System.out.println(dia.getMessage());
> }
> }
> Resource resource = targetSchema.eResource();
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> public void selectionChanged(IAction action, ISelection selection) {
> }
>
> }
> ------------------------------------------------------------ --
> Test1.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.example.org/test1"
> xmlns:tns1="http://www.example.org/test1" elementFormDefault="qualified">
>
> <element name="ele1" type="tns1:simple1" nillable="true"/>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
> ------------------------------------------------------------ --
> Test2.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
> <element name="ele1" type="tns:simple1" />
>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
>
> ------------------------------------------------------------ -------
> Test2.xsd (After Code execution)
> ------------------------------------------------------------ -------
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
> <element name="ele1" nillable="true" type="tns1:simple1"/>
>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
>
> ------------------------------------------------------------ ------
> Validation falis with below message
> ------------------------------------------------------------ ------
> XSD: Type reference 'tns1#simple1' is unresolved, which is true as
> replacechild() method should have defined "tns1" in element ?
>
> Am i doing something wrong or is there any other way to do this.
>
> --
> Thanks and Regards,
> Bhuvan Mehta
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem during replacing element from one schema to another schema using DOM [message #77706 is a reply to message #77668] Sat, 16 May 2009 13:59 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Bhuvan,

Keep in mind that DOM doesn't know that type="..." contains a QName so
don't expect it to handle that the same was as when a QName is used in
an element or attribute. As far as DOM is concerned the values of
attributes are always just strings. So no, don't expect this to work
without effort...


Bhuvan wrote:
> Hello,
>
> I am trying to replace an element from one schema(source) to another
> schema(target) schema using DOM APIs as below :
> and the replacement is not happening properly.
>
> ------------------------------------------------------------ --
> TestContentCopy.java
> ------------------------------------------------------------ --
> package test.schema;
>
> import java.io.File;
> import java.io.IOException;
> import java.util.Collection;
> import java.util.Collections;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.jface.action.IAction;
> import org.eclipse.jface.viewers.ISelection;
> import org.eclipse.ui.IObjectActionDelegate;
> import org.eclipse.ui.IWorkbenchPart;
> import org.eclipse.xsd.XSDDiagnostic;
> import org.eclipse.xsd.XSDElementDeclaration;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.util.XSDResourceImpl;
> import org.w3c.dom.Element;
> import org.w3c.dom.Node;
>
> public class TestContentCopy implements IObjectActionDelegate {
>
> public void setActivePart(IAction action, IWorkbenchPart targetPart) {
> }
>
> public void run(IAction action) {
>
> String sourceLoc = "C:\\test1.xsd";
> String targetLoc = "C:\\test2.xsd";
>
> File file1 = new File(sourceLoc);
> File file2 = new File(targetLoc);
>
> final ResourceSet resourceSet = new ResourceSetImpl();
> final Resource res1 =
> resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
> final Resource res2 =
> resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);
>
> XSDSchema sourceSchema = ((XSDResourceImpl) res1).getSchema();
> XSDSchema targetSchema = ((XSDResourceImpl) res2).getSchema();
>
> XSDElementDeclaration srcComponent =
> sourceSchema.getElementDeclarations().get(0);
> XSDElementDeclaration oldComponent =
> targetSchema.getElementDeclarations().get(0);
>
> Element element = srcComponent.getElement();
> if (element != null) {
> final Node newComponent =
> targetSchema.getDocument().importNode(srcComponent.getElemen t(), true);
> targetSchema.getElement().replaceChild(newComponent,
> oldComponent.getElement());
> targetSchema.getDocument().normalizeDocument();
> }
>
> targetSchema.validate();
> final Collection<XSDDiagnostic> diag = targetSchema.getAllDiagnostics();
> if (diag != null && !diag.isEmpty()) {
> for (final XSDDiagnostic dia : diag) {
> System.out.println(dia.getMessage());
> }
> }
> Resource resource = targetSchema.eResource();
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> public void selectionChanged(IAction action, ISelection selection) {
> }
>
> }
> ------------------------------------------------------------ --
> Test1.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.example.org/test1"
> xmlns:tns1="http://www.example.org/test1" elementFormDefault="qualified">
>
> <element name="ele1" type="tns1:simple1" nillable="true"/>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
> ------------------------------------------------------------ --
> Test2.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
> <element name="ele1" type="tns:simple1" />
>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
>
> ------------------------------------------------------------ -------
> Test2.xsd (After Code execution)
> ------------------------------------------------------------ -------
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
> <element name="ele1" nillable="true" type="tns1:simple1"/>
>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
>
> ------------------------------------------------------------ ------
> Validation falis with below message
> ------------------------------------------------------------ ------
> XSD: Type reference 'tns1#simple1' is unresolved, which is true as
> replacechild() method should have defined "tns1" in element ?
>
> Am i doing something wrong or is there any other way to do this.
>
> --
> Thanks and Regards,
> Bhuvan Mehta
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem during replacing element from one schema to another schema using DOM [message #604189 is a reply to message #77668] Fri, 15 May 2009 19:50 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Bhuvan,

It's unlikely I'll have time to look at this before June 26th,
especially if I have to set up a project to even run this scenario.


Bhuvan wrote:
> Hello,
>
> I am trying to replace an element from one schema(source) to another
> schema(target) schema using DOM APIs as below :
> and the replacement is not happening properly.
>
> ------------------------------------------------------------ --
> TestContentCopy.java
> ------------------------------------------------------------ --
> package test.schema;
>
> import java.io.File;
> import java.io.IOException;
> import java.util.Collection;
> import java.util.Collections;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.jface.action.IAction;
> import org.eclipse.jface.viewers.ISelection;
> import org.eclipse.ui.IObjectActionDelegate;
> import org.eclipse.ui.IWorkbenchPart;
> import org.eclipse.xsd.XSDDiagnostic;
> import org.eclipse.xsd.XSDElementDeclaration;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.util.XSDResourceImpl;
> import org.w3c.dom.Element;
> import org.w3c.dom.Node;
>
> public class TestContentCopy implements IObjectActionDelegate {
>
> public void setActivePart(IAction action, IWorkbenchPart targetPart) {
> }
>
> public void run(IAction action) {
>
> String sourceLoc = "C:\\test1.xsd";
> String targetLoc = "C:\\test2.xsd";
>
> File file1 = new File(sourceLoc);
> File file2 = new File(targetLoc);
>
> final ResourceSet resourceSet = new ResourceSetImpl();
> final Resource res1 =
> resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
> final Resource res2 =
> resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);
>
> XSDSchema sourceSchema = ((XSDResourceImpl) res1).getSchema();
> XSDSchema targetSchema = ((XSDResourceImpl) res2).getSchema();
>
> XSDElementDeclaration srcComponent =
> sourceSchema.getElementDeclarations().get(0);
> XSDElementDeclaration oldComponent =
> targetSchema.getElementDeclarations().get(0);
>
> Element element = srcComponent.getElement();
> if (element != null) {
> final Node newComponent =
> targetSchema.getDocument().importNode(srcComponent.getElemen t(), true);
> targetSchema.getElement().replaceChild(newComponent,
> oldComponent.getElement());
> targetSchema.getDocument().normalizeDocument();
> }
>
> targetSchema.validate();
> final Collection<XSDDiagnostic> diag = targetSchema.getAllDiagnostics();
> if (diag != null && !diag.isEmpty()) {
> for (final XSDDiagnostic dia : diag) {
> System.out.println(dia.getMessage());
> }
> }
> Resource resource = targetSchema.eResource();
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> public void selectionChanged(IAction action, ISelection selection) {
> }
>
> }
> ------------------------------------------------------------ --
> Test1.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.example.org/test1"
> xmlns:tns1="http://www.example.org/test1" elementFormDefault="qualified">
>
> <element name="ele1" type="tns1:simple1" nillable="true"/>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
> ------------------------------------------------------------ --
> Test2.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
> <element name="ele1" type="tns:simple1" />
>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
>
> ------------------------------------------------------------ -------
> Test2.xsd (After Code execution)
> ------------------------------------------------------------ -------
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
> <element name="ele1" nillable="true" type="tns1:simple1"/>
>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
>
> ------------------------------------------------------------ ------
> Validation falis with below message
> ------------------------------------------------------------ ------
> XSD: Type reference 'tns1#simple1' is unresolved, which is true as
> replacechild() method should have defined "tns1" in element ?
>
> Am i doing something wrong or is there any other way to do this.
>
> --
> Thanks and Regards,
> Bhuvan Mehta
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Problem during replacing element from one schema to another schema using DOM [message #604194 is a reply to message #77668] Sat, 16 May 2009 13:59 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Bhuvan,

Keep in mind that DOM doesn't know that type="..." contains a QName so
don't expect it to handle that the same was as when a QName is used in
an element or attribute. As far as DOM is concerned the values of
attributes are always just strings. So no, don't expect this to work
without effort...


Bhuvan wrote:
> Hello,
>
> I am trying to replace an element from one schema(source) to another
> schema(target) schema using DOM APIs as below :
> and the replacement is not happening properly.
>
> ------------------------------------------------------------ --
> TestContentCopy.java
> ------------------------------------------------------------ --
> package test.schema;
>
> import java.io.File;
> import java.io.IOException;
> import java.util.Collection;
> import java.util.Collections;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.jface.action.IAction;
> import org.eclipse.jface.viewers.ISelection;
> import org.eclipse.ui.IObjectActionDelegate;
> import org.eclipse.ui.IWorkbenchPart;
> import org.eclipse.xsd.XSDDiagnostic;
> import org.eclipse.xsd.XSDElementDeclaration;
> import org.eclipse.xsd.XSDSchema;
> import org.eclipse.xsd.util.XSDResourceImpl;
> import org.w3c.dom.Element;
> import org.w3c.dom.Node;
>
> public class TestContentCopy implements IObjectActionDelegate {
>
> public void setActivePart(IAction action, IWorkbenchPart targetPart) {
> }
>
> public void run(IAction action) {
>
> String sourceLoc = "C:\\test1.xsd";
> String targetLoc = "C:\\test2.xsd";
>
> File file1 = new File(sourceLoc);
> File file2 = new File(targetLoc);
>
> final ResourceSet resourceSet = new ResourceSetImpl();
> final Resource res1 =
> resourceSet.getResource(URI.createURI(file1.toURI().toString ()), true);
> final Resource res2 =
> resourceSet.getResource(URI.createURI(file2.toURI().toString ()), true);
>
> XSDSchema sourceSchema = ((XSDResourceImpl) res1).getSchema();
> XSDSchema targetSchema = ((XSDResourceImpl) res2).getSchema();
>
> XSDElementDeclaration srcComponent =
> sourceSchema.getElementDeclarations().get(0);
> XSDElementDeclaration oldComponent =
> targetSchema.getElementDeclarations().get(0);
>
> Element element = srcComponent.getElement();
> if (element != null) {
> final Node newComponent =
> targetSchema.getDocument().importNode(srcComponent.getElemen t(), true);
> targetSchema.getElement().replaceChild(newComponent,
> oldComponent.getElement());
> targetSchema.getDocument().normalizeDocument();
> }
>
> targetSchema.validate();
> final Collection<XSDDiagnostic> diag = targetSchema.getAllDiagnostics();
> if (diag != null && !diag.isEmpty()) {
> for (final XSDDiagnostic dia : diag) {
> System.out.println(dia.getMessage());
> }
> }
> Resource resource = targetSchema.eResource();
> try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> public void selectionChanged(IAction action, ISelection selection) {
> }
>
> }
> ------------------------------------------------------------ --
> Test1.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.example.org/test1"
> xmlns:tns1="http://www.example.org/test1" elementFormDefault="qualified">
>
> <element name="ele1" type="tns1:simple1" nillable="true"/>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
> ------------------------------------------------------------ --
> Test2.xsd
> ------------------------------------------------------------ --
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
> <element name="ele1" type="tns:simple1" />
>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
>
> ------------------------------------------------------------ -------
> Test2.xsd (After Code execution)
> ------------------------------------------------------------ -------
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:tns="http://www.example.org/test1"
> elementFormDefault="qualified"
> targetNamespace="http://www.example.org/test1">
>
> <element name="ele1" nillable="true" type="tns1:simple1"/>
>
> <simpleType name="simple1">
> <union memberTypes="int">
> <annotation>
> <documentation>
> SimpleType.
> </documentation>
> </annotation>
> </union>
> </simpleType>
>
> </schema>
>
> ------------------------------------------------------------ ------
> Validation falis with below message
> ------------------------------------------------------------ ------
> XSD: Type reference 'tns1#simple1' is unresolved, which is true as
> replacechild() method should have defined "tns1" in element ?
>
> Am i doing something wrong or is there any other way to do this.
>
> --
> Thanks and Regards,
> Bhuvan Mehta
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Problem during replacing element from one schema to another schema using DOM
Next Topic:escaping chars in XSD
Goto Forum:
  


Current Time: Tue Apr 23 17:59:21 GMT 2024

Powered by FUDForum. Page generated in 0.03265 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top