Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » XML Schema Definition (XSD) » Namespace Resolution
Namespace Resolution [message #7022] Fri, 13 December 2002 19:42 Go to next message
Eclipse UserFriend
Originally posted by: dsingh.altoweb.com

Hi all,

I am wondering if something is possible with the current XSD API.

I have a WSDL document similar in structure to the following,

<definitions xmlns:a='http://mynamespace' xmlns
xmlns:s='http://www.w3.org/2001/XMLSchema'>
<types>
<s:schema targetNamespace="http://mynamespace">
<s:element name="myElement" type="string"/>
</s:schema>
</types>
</definitions>

If I extract the schema content and attempt to parse it using the xsd parser
I understand it will fail because it will not be able to resolve the prefix
s. However in this situation is it possible to pass in a set of
prefix/namespace pairs to help with the resolution.

Or is there another way to handle this situation.

Thanks

Dharminder Singh
Re: Namespace Resolution [message #7092 is a reply to message #7022] Fri, 13 December 2002 21:44 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Dharminder,

I would suggest parsing the whole thing as a DOM. You can then navigate to the
<schema> element(s), create an XSDSchema, and do
xsdSchema.setElement(schemaElement). Please see the response to Mark Carman
question, just two notes up. ;-)

I'll bet a lot of people would like it if there were an XSDResourceImpl that
handled .wsdl; I wish I had more time...


Dharminder Singh wrote:

> Hi all,
>
> I am wondering if something is possible with the current XSD API.
>
> I have a WSDL document similar in structure to the following,
>
> <definitions xmlns:a='http://mynamespace' xmlns
> xmlns:s='http://www.w3.org/2001/XMLSchema'>
> <types>
> <s:schema targetNamespace="http://mynamespace">
> <s:element name="myElement" type="string"/>
> </s:schema>
> </types>
> </definitions>
>
> If I extract the schema content and attempt to parse it using the xsd parser
> I understand it will fail because it will not be able to resolve the prefix
> s. However in this situation is it possible to pass in a set of
> prefix/namespace pairs to help with the resolution.
>
> Or is there another way to handle this situation.
>
> Thanks
>
> Dharminder Singh

--
Ed Merks
Re: Namespace Resolution (WSDL documents) [message #7162 is a reply to message #7092] Mon, 23 December 2002 17:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: carman.itc.it

Dear Dharminder, Ed

First of all, thanks a lot to Ed for answering all our questions so
promptly!

I had to deal with the same problem as Dharminder when attempting to load
schemas defined within WSDL documents - namely that sometimes namespace
prefix definitions are found in the root <wsdl:definitions> element and
not repeated in the <xsd:schema> element. One work-around is to first
parse the WSDL document using a DOM parser (which you need to do anyway)
and then simply copy the missing definitions into the <schema> element. -
See attached code.

Secondly, I was unsuccessful in loading the <schema> element directly into
XSD by using the XSDSchema.setElement() method. (Even when calling the
XSDSchema.setSchemaLocation() method as described in the previous mail). I
could query the schema, but all the links to type-definitions were broken.
The work-around I used was to print-out the DOM <schema> element to a
temporary file (see attached code), and then load it as you would a normal
schema document.

A question for Ed:
When resolving type definitions, if the corresponding type definition is
not found, a new XSDSimpleTypeDefinition is returned. Wouldn't it be
better if it returned a null pointer or some sort of exception? As it is,
it is a bit difficult to know whether namespace links are working or not,
i.e. whether the returned definition is the real one or not.

Thanks,

Mark Carman



// some code for loading schemas from WSDL documents

public static XSDSchema loadSchemaFromWSDL(String wsdlURL) {

try {

// load WSDL document into DOM
javax.xml.parsers.DocumentBuilderFactory f =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
f.setNamespaceAware(true);
Document doc = f.newDocumentBuilder().parse(wsdlURL);

// get the attributes (namespace declarations) from the root node
NamedNodeMap m = doc.getDocumentElement().getAttributes();

// find the <schema> element (ignoring namespace prefix)
Element schemaElement = (Element)
doc.getDocumentElement().getElementsByTagNameNS("*","schema ").item(0);

// add any missing namespace declarations to schema element
for (int i = 0; i < m.getLength(); i++) {
if
(m.item(i).getNodeName().startsWith("xmlns:" )&&!schemaElement.hasAttribute(m.item(i).getNodeName ()))
{

schemaElement.setAttribute(m.item(i).getNodeName(),m.item(i) .getNodeValue());
}
}

// write the <schema> node to a temporary file as though it were a
schema document
org.apache.xml.serialize.DOMWriterImpl d = new
org.apache.xml.serialize.DOMWriterImpl(true);
String tempSchemaFile = "/tmp/schema.xsd";
d.writeNode(new FileOutputStream(new
File(tempSchemaFile)),schemaElement);


// now load from schema from a file in normal fashion

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
new XSDResourceFactoryImpl());
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
Boolean.TRUE);

// create URI from relative file path
URI schemaURI = URI.createFileURI(new
java.io.File(tempSchemaFile).getAbsoluteFile().getCanonicalP ath());
XSDResourceImpl xsdSchemaResource =
(XSDResourceImpl)resourceSet.getResource(schemaURI, true);

// find the schema from amongst the resources
Iterator resources = resourceSet.getResources().iterator();
while (resources.hasNext()) {
Resource resource = (Resource) resources.next();
if (resource instanceof XSDResourceImpl) {
return ((XSDResourceImpl) resource).getSchema();
}
}

}
catch (Exception e) {
e.printStackTrace();
}

return null;

}
Re: Namespace Resolution (WSDL documents) [message #7260 is a reply to message #7162] Tue, 07 January 2003 01:23 Go to previous message
Dave Spriet is currently offline Dave SprietFriend
Messages: 14
Registered: July 2009
Junior Member
Hello Mark,

I know you were asking Ed but I will answer this. Yes when resolving type
definitions or if you resolve any object in the XML Schema
model you will be returned a dummy object if it can not be resolved. This
is actually very powerful because then you do not have to take care of null
or NullPointers everywhere in your code.

If you want you could do an eContainer() on the XSDElementDeclaration's type
definition and if it returns null then it is a dummy (unresolved) type.

Cheers,
Dave



"Mark Carman" <carman@itc.it> wrote in message
news:au7hko$e6l$1@rogue.oti.com...
> Dear Dharminder, Ed
>
> First of all, thanks a lot to Ed for answering all our questions so
> promptly!
>
> I had to deal with the same problem as Dharminder when attempting to load
> schemas defined within WSDL documents - namely that sometimes namespace
> prefix definitions are found in the root <wsdl:definitions> element and
> not repeated in the <xsd:schema> element. One work-around is to first
> parse the WSDL document using a DOM parser (which you need to do anyway)
> and then simply copy the missing definitions into the <schema> element. -
> See attached code.
>
> Secondly, I was unsuccessful in loading the <schema> element directly into
> XSD by using the XSDSchema.setElement() method. (Even when calling the
> XSDSchema.setSchemaLocation() method as described in the previous mail). I
> could query the schema, but all the links to type-definitions were broken.
> The work-around I used was to print-out the DOM <schema> element to a
> temporary file (see attached code), and then load it as you would a normal
> schema document.
>
> A question for Ed:
> When resolving type definitions, if the corresponding type definition is
> not found, a new XSDSimpleTypeDefinition is returned. Wouldn't it be
> better if it returned a null pointer or some sort of exception? As it is,
> it is a bit difficult to know whether namespace links are working or not,
> i.e. whether the returned definition is the real one or not.
>
> Thanks,
>
> Mark Carman
>
>
>
> // some code for loading schemas from WSDL documents
>
> public static XSDSchema loadSchemaFromWSDL(String wsdlURL) {
>
> try {
>
> // load WSDL document into DOM
> javax.xml.parsers.DocumentBuilderFactory f =
> javax.xml.parsers.DocumentBuilderFactory.newInstance();
> f.setNamespaceAware(true);
> Document doc = f.newDocumentBuilder().parse(wsdlURL);
>
> // get the attributes (namespace declarations) from the root node
> NamedNodeMap m = doc.getDocumentElement().getAttributes();
>
> // find the <schema> element (ignoring namespace prefix)
> Element schemaElement = (Element)
> doc.getDocumentElement().getElementsByTagNameNS("*","schema ").item(0);
>
> // add any missing namespace declarations to schema element
> for (int i = 0; i < m.getLength(); i++) {
> if
>
(m.item(i).getNodeName().startsWith("xmlns:")&&!schemaElement.hasAttribute(m
..item(i).getNodeName()))
> {
>
>
schemaElement.setAttribute(m.item(i).getNodeName(),m.item(i) .getNodeValue())
;
> }
> }
>
> // write the <schema> node to a temporary file as though it were a
> schema document
> org.apache.xml.serialize.DOMWriterImpl d = new
> org.apache.xml.serialize.DOMWriterImpl(true);
> String tempSchemaFile = "/tmp/schema.xsd";
> d.writeNode(new FileOutputStream(new
> File(tempSchemaFile)),schemaElement);
>
>
> // now load from schema from a file in normal fashion
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
> new XSDResourceFactoryImpl());
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
> Boolean.TRUE);
>
> // create URI from relative file path
> URI schemaURI = URI.createFileURI(new
> java.io.File(tempSchemaFile).getAbsoluteFile().getCanonicalP ath());
> XSDResourceImpl xsdSchemaResource =
> (XSDResourceImpl)resourceSet.getResource(schemaURI, true);
>
> // find the schema from amongst the resources
> Iterator resources = resourceSet.getResources().iterator();
> while (resources.hasNext()) {
> Resource resource = (Resource) resources.next();
> if (resource instanceof XSDResourceImpl) {
> return ((XSDResourceImpl) resource).getSchema();
> }
> }
>
> }
> catch (Exception e) {
> e.printStackTrace();
> }
>
> return null;
>
> }
>
>
Re: Namespace Resolution [message #564193 is a reply to message #7022] Fri, 13 December 2002 21:44 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Dharminder,

I would suggest parsing the whole thing as a DOM. You can then navigate to the
<schema> element(s), create an XSDSchema, and do
xsdSchema.setElement(schemaElement). Please see the response to Mark Carman
question, just two notes up. ;-)

I'll bet a lot of people would like it if there were an XSDResourceImpl that
handled .wsdl; I wish I had more time...


Dharminder Singh wrote:

> Hi all,
>
> I am wondering if something is possible with the current XSD API.
>
> I have a WSDL document similar in structure to the following,
>
> <definitions xmlns:a='http://mynamespace' xmlns
> xmlns:s='http://www.w3.org/2001/XMLSchema'>
> <types>
> <s:schema targetNamespace="http://mynamespace">
> <s:element name="myElement" type="string"/>
> </s:schema>
> </types>
> </definitions>
>
> If I extract the schema content and attempt to parse it using the xsd parser
> I understand it will fail because it will not be able to resolve the prefix
> s. However in this situation is it possible to pass in a set of
> prefix/namespace pairs to help with the resolution.
>
> Or is there another way to handle this situation.
>
> Thanks
>
> Dharminder Singh

--
Ed Merks


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Namespace Resolution (WSDL documents) [message #564270 is a reply to message #7092] Mon, 23 December 2002 17:37 Go to previous message
Eclipse UserFriend
Originally posted by: carman.itc.it

Dear Dharminder, Ed

First of all, thanks a lot to Ed for answering all our questions so
promptly!

I had to deal with the same problem as Dharminder when attempting to load
schemas defined within WSDL documents - namely that sometimes namespace
prefix definitions are found in the root <wsdl:definitions> element and
not repeated in the <xsd:schema> element. One work-around is to first
parse the WSDL document using a DOM parser (which you need to do anyway)
and then simply copy the missing definitions into the <schema> element. -
See attached code.

Secondly, I was unsuccessful in loading the <schema> element directly into
XSD by using the XSDSchema.setElement() method. (Even when calling the
XSDSchema.setSchemaLocation() method as described in the previous mail). I
could query the schema, but all the links to type-definitions were broken.
The work-around I used was to print-out the DOM <schema> element to a
temporary file (see attached code), and then load it as you would a normal
schema document.

A question for Ed:
When resolving type definitions, if the corresponding type definition is
not found, a new XSDSimpleTypeDefinition is returned. Wouldn't it be
better if it returned a null pointer or some sort of exception? As it is,
it is a bit difficult to know whether namespace links are working or not,
i.e. whether the returned definition is the real one or not.

Thanks,

Mark Carman



// some code for loading schemas from WSDL documents

public static XSDSchema loadSchemaFromWSDL(String wsdlURL) {

try {

// load WSDL document into DOM
javax.xml.parsers.DocumentBuilderFactory f =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
f.setNamespaceAware(true);
Document doc = f.newDocumentBuilder().parse(wsdlURL);

// get the attributes (namespace declarations) from the root node
NamedNodeMap m = doc.getDocumentElement().getAttributes();

// find the <schema> element (ignoring namespace prefix)
Element schemaElement = (Element)
doc.getDocumentElement().getElementsByTagNameNS("*","schema ").item(0);

// add any missing namespace declarations to schema element
for (int i = 0; i < m.getLength(); i++) {
if
(m.item(i).getNodeName().startsWith("xmlns:" )&&!schemaElement.hasAttribute(m.item(i).getNodeName ()))
{

schemaElement.setAttribute(m.item(i).getNodeName(),m.item(i) .getNodeValue());
}
}

// write the <schema> node to a temporary file as though it were a
schema document
org.apache.xml.serialize.DOMWriterImpl d = new
org.apache.xml.serialize.DOMWriterImpl(true);
String tempSchemaFile = "/tmp/schema.xsd";
d.writeNode(new FileOutputStream(new
File(tempSchemaFile)),schemaElement);


// now load from schema from a file in normal fashion

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
new XSDResourceFactoryImpl());
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
Boolean.TRUE);

// create URI from relative file path
URI schemaURI = URI.createFileURI(new
java.io.File(tempSchemaFile).getAbsoluteFile().getCanonicalP ath());
XSDResourceImpl xsdSchemaResource =
(XSDResourceImpl)resourceSet.getResource(schemaURI, true);

// find the schema from amongst the resources
Iterator resources = resourceSet.getResources().iterator();
while (resources.hasNext()) {
Resource resource = (Resource) resources.next();
if (resource instanceof XSDResourceImpl) {
return ((XSDResourceImpl) resource).getSchema();
}
}

}
catch (Exception e) {
e.printStackTrace();
}

return null;

}
Re: Namespace Resolution (WSDL documents) [message #564368 is a reply to message #7162] Tue, 07 January 2003 01:23 Go to previous message
Dave Spriet is currently offline Dave SprietFriend
Messages: 14
Registered: July 2009
Junior Member
Hello Mark,

I know you were asking Ed but I will answer this. Yes when resolving type
definitions or if you resolve any object in the XML Schema
model you will be returned a dummy object if it can not be resolved. This
is actually very powerful because then you do not have to take care of null
or NullPointers everywhere in your code.

If you want you could do an eContainer() on the XSDElementDeclaration's type
definition and if it returns null then it is a dummy (unresolved) type.

Cheers,
Dave



"Mark Carman" <carman@itc.it> wrote in message
news:au7hko$e6l$1@rogue.oti.com...
> Dear Dharminder, Ed
>
> First of all, thanks a lot to Ed for answering all our questions so
> promptly!
>
> I had to deal with the same problem as Dharminder when attempting to load
> schemas defined within WSDL documents - namely that sometimes namespace
> prefix definitions are found in the root <wsdl:definitions> element and
> not repeated in the <xsd:schema> element. One work-around is to first
> parse the WSDL document using a DOM parser (which you need to do anyway)
> and then simply copy the missing definitions into the <schema> element. -
> See attached code.
>
> Secondly, I was unsuccessful in loading the <schema> element directly into
> XSD by using the XSDSchema.setElement() method. (Even when calling the
> XSDSchema.setSchemaLocation() method as described in the previous mail). I
> could query the schema, but all the links to type-definitions were broken.
> The work-around I used was to print-out the DOM <schema> element to a
> temporary file (see attached code), and then load it as you would a normal
> schema document.
>
> A question for Ed:
> When resolving type definitions, if the corresponding type definition is
> not found, a new XSDSimpleTypeDefinition is returned. Wouldn't it be
> better if it returned a null pointer or some sort of exception? As it is,
> it is a bit difficult to know whether namespace links are working or not,
> i.e. whether the returned definition is the real one or not.
>
> Thanks,
>
> Mark Carman
>
>
>
> // some code for loading schemas from WSDL documents
>
> public static XSDSchema loadSchemaFromWSDL(String wsdlURL) {
>
> try {
>
> // load WSDL document into DOM
> javax.xml.parsers.DocumentBuilderFactory f =
> javax.xml.parsers.DocumentBuilderFactory.newInstance();
> f.setNamespaceAware(true);
> Document doc = f.newDocumentBuilder().parse(wsdlURL);
>
> // get the attributes (namespace declarations) from the root node
> NamedNodeMap m = doc.getDocumentElement().getAttributes();
>
> // find the <schema> element (ignoring namespace prefix)
> Element schemaElement = (Element)
> doc.getDocumentElement().getElementsByTagNameNS("*","schema ").item(0);
>
> // add any missing namespace declarations to schema element
> for (int i = 0; i < m.getLength(); i++) {
> if
>
(m.item(i).getNodeName().startsWith("xmlns:")&&!schemaElement.hasAttribute(m
..item(i).getNodeName()))
> {
>
>
schemaElement.setAttribute(m.item(i).getNodeName(),m.item(i) .getNodeValue())
;
> }
> }
>
> // write the <schema> node to a temporary file as though it were a
> schema document
> org.apache.xml.serialize.DOMWriterImpl d = new
> org.apache.xml.serialize.DOMWriterImpl(true);
> String tempSchemaFile = "/tmp/schema.xsd";
> d.writeNode(new FileOutputStream(new
> File(tempSchemaFile)),schemaElement);
>
>
> // now load from schema from a file in normal fashion
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd",
> new XSDResourceFactoryImpl());
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION,
> Boolean.TRUE);
>
> // create URI from relative file path
> URI schemaURI = URI.createFileURI(new
> java.io.File(tempSchemaFile).getAbsoluteFile().getCanonicalP ath());
> XSDResourceImpl xsdSchemaResource =
> (XSDResourceImpl)resourceSet.getResource(schemaURI, true);
>
> // find the schema from amongst the resources
> Iterator resources = resourceSet.getResources().iterator();
> while (resources.hasNext()) {
> Resource resource = (Resource) resources.next();
> if (resource instanceof XSDResourceImpl) {
> return ((XSDResourceImpl) resource).getSchema();
> }
> }
>
> }
> catch (Exception e) {
> e.printStackTrace();
> }
>
> return null;
>
> }
>
>
Previous Topic:Is there a <xsd:all> limitation?
Next Topic:Is there a <xsd:all> limitation?
Goto Forum:
  


Current Time: Sat Apr 27 05:01:33 GMT 2024

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

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

Back to the top