Home » Archived » XML Schema Definition (XSD) » Re: Problem when extracting XSD from WSDL 
| Re: Problem when extracting XSD from WSDL [message #64866] | 
Thu, 03 November 2005 06:37  | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Originally posted by: merks.ca.ibm.com 
 
This is a multi-part message in MIME format. 
--------------020404050508090906030409 
Content-Type: text/plain; charset=ISO-8859-1; format=flowed 
Content-Transfer-Encoding: 7bit 
 
Vladimir, 
 
Don't use backslash in a URI.  It's just treated as a regular character,  
not as "/". 
 
    createURI("file:///C:\\temp\\extracted.xsd") 
 
Also, don't use org.eclipse.xsd.impl, i.e., the XSD API implementation  
classes directly either; they are not API and can be changed arbitrarily. 
 
The save method of an XSDResourceImpl is like this: 
 
      protected void doSave(OutputStream os, Map options) throws IOException 
      { 
        XSDSchema xsdSchema = getSchema(); 
        if (xsdSchema != null) 
        { 
          Document document = xsdSchema.*getDocument*(); 
          if (document == null) 
          { 
            xsdSchema.updateDocument(); 
            document = xsdSchema.getDocument(); 
          } 
 
          if (xsdSchema.getElement() == null) 
          { 
            xsdSchema.updateElement(); 
          } 
 
          doSerialize(os, document, options == null ? null : 
    (String)options.get(XSD_ENCODING)); 
        } 
      } 
 
Note how it saves the entire document that contains the schema's element  
(because XSDResourceImpl can be used to read a .wsdl directly and will  
wrap all the XSDSchemas in that document, which you can then edit, and  
then save the whole original document again). 
 
You need to extract the schema element into a new Document if you want  
to save just the schema.  Note that  
XSDResourceImpl.serialize(<outputStream>, exElement.getElement()) or  
your save(Node) method  would accomplish the same thing as you are  
doing, but more cheaply, so unless you are actually going to manipulate  
the XSDSchema, this seems an expensive way.  Perhaps just serializing  
the element wouldn't write out all the necessary prefixes declared in a  
outer scopes? 
 
 
Vladimir Isajkin wrote: 
 
> Hi, 
> I extract XSD schema from WSDL as shown in the code below and then try  
> to save it. But instead of XSD the complete content of WSDL is saved. 
> If I save DOM element using XML Transformer API then I get saved only  
> my schema. 
> 
> What am I doing wrong? 
> 
> Because I intend to use the extracted schema in my next code I need it  
> as XSDSchema instance. 
> 
> 
> import java.io.File; 
> import java.util.Collections; 
> import java.util.Iterator; 
> import java.util.List; 
> 
> import javax.wsdl.Definition; 
> import javax.wsdl.WSDLException; 
> import javax.wsdl.extensions.UnknownExtensibilityElement; 
> import javax.wsdl.factory.WSDLFactory; 
> import javax.xml.transform.Result; 
> import javax.xml.transform.Source; 
> import javax.xml.transform.Transformer; 
> import javax.xml.transform.TransformerConfigurationException; 
> import javax.xml.transform.TransformerException; 
> import javax.xml.transform.TransformerFactory; 
> import javax.xml.transform.TransformerFactoryConfigurationError; 
> import javax.xml.transform.dom.DOMSource; 
> import javax.xml.transform.stream.StreamResult; 
> 
> 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.xsd.XSDFactory; 
> import org.eclipse.xsd.XSDSchema; 
> import org.eclipse.xsd.impl.XSDSchemaImpl; 
> import org.eclipse.xsd.util.XSDResourceImpl; 
> import org.w3c.dom.Node; 
> 
> 
> public class WsdlHandler { 
>     private Definition wsdlDefinition; 
> 
>     public static void main(String[] args) { 
>         WsdlHandler handler = new                               
> WsdlHandler("ResourceIdentification.wsdl"); 
>         handler.extractXSD(); 
>     } 
> 
>     public WsdlHandler(String wsdlFile) { 
>         try { 
>             wsdlDefinition = WSDLFactory.newInstance(). 
>             newWSDLReader().readWSDL(wsdlFile); 
>         } catch (WSDLException exception) { 
>             exception.printStackTrace(); 
>         } 
>     } 
> 
> 
>     private void extractXSD() { 
>         List exList = wsdlDefinition.getTypes(). 
>             getExtensibilityElements(); 
>         if (exList != null) { 
>             for (Iterator iterator =  
> exList.iterator();                                   
> iterator.hasNext();) { 
>                 UnknownExtensibilityElement exElement  
> =                      (UnknownExtensibilityElement) iterator.next(); 
> 
>                 XSDSchema schema =XSDSchemaImpl. 
>                 createSchema(exElement.getElement()); 
>                 //OR 
> //                XSDSchema schema = XSDFactory.  
> eINSTANCE.                                  createXSDSchema(); 
> //                schema.setElement(exElement); 
>                 schema.updateElement(); 
>                 schema.update(); 
>                 try { 
>                     ResourceSet resourceSet = new ResourceSetImpl(); 
>                     Resource resource = new XSDResourceImpl(URI 
>                           createURI("file:///C:\\temp\\extracted.xsd")); 
>                     resource.getContents().add(schema); 
>                     resourceSet.getResources().add(resource); 
>                     resource.save(Collections.EMPTY_MAP); 
>                 } catch (Exception exception) { 
>                     System.out.println(exception.getLocalizedMessage()); 
>                     exception.printStackTrace(); 
>                 } 
>                 save(exElement.getElement()); 
>             } 
>         } 
>     } 
>     private void save(Node node) { 
>         try { 
>             File outputFile = new File("C:\\temp\\element.xsd"); 
>             Source source = new DOMSource(node); 
>             Result result = new StreamResult(outputFile); 
>             Transformer xformer =                               
> TransformerFactory.newInstance().newTransformer(); 
>             xformer.transform(source, result); 
>         } catch (TransformerConfigurationException e) { 
>             e.printStackTrace(); 
>         } catch (TransformerFactoryConfigurationError e) { 
>             e.printStackTrace(); 
>         } catch (TransformerException e) { 
>             e.printStackTrace(); 
>         } 
>     } 
> } 
> 
> ------------------------------------------------------------ ------------ 
> 
><?xml version="1.0" encoding="UTF-8"?> 
><!-- begin_generated_IBM_copyright_prolog		                    --> 
><!--                                                                  --> 
><!-- This is an automatically generated copyright prolog              --> 
><!-- After initializing, DO NOT MODIFY OR MOVE                        --> 
><!--                                                                  --> 
><!--                                                                  --> 
><!--                                                                  --> 
><!-- Licensed Materials - Property of IBM                             --> 
><!-- Restricted Materials of IBM                                      --> 
><!--                                                                  --> 
><!-- Product(s):                                                      --> 
><!--                                                                  --> 
><!-- (C)Copyright IBM Corp.  2005, 2005                               --> 
><!-- All Rights Reserved                                              --> 
><!--                                                                  -->  
><!-- US Government Users Restricted Rights - Use, duplication or      --> 
><!-- disclosure restriced by GSA ADP Schedule Contract with IBM Corp. --> 
><!--                                                                  --> 
><!--                                                                  --> 
><!--                                                                  --> 
><!-- end_generated_IBM_copyright_prolog                               --> 
><wsdl:definitions name="ResourceIdentification"  
>targetNamespace=" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.wsdl" 
>    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"  
>    xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
>    xmlns:wsrf-rp=" http://docs.oasis-open.org/wsrf/2004/11/wsrf-WS-ResourceProp erties-1.2-draft-05.xsd" 
>    xmlns="http://schemas.xmlsoap.org/wsdl/"> 
>	<wsdl:types> 
>		<xsd:schema targetNamespace=" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.wsdl" 
>            xmlns:rvs-mr-ri=" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.xsd" 
>            xmlns="http://www.w3.org/2001/XMLSchema" 
>            xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
>        <xsd:import namespace=" http://docs.oasis-open.org/wsrf/2004/11/wsrf-WS-ResourceProp erties-1.2-draft-05.xsd" 
>            schemaLocation="WS-ResourceProperties.xsd"/> 
>		<xsd:import namespace=" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.xsd" 
>             schemaLocation="ResourceIdentification.xsd"/> 
>		</xsd:schema> 
>	</wsdl:types> 
>	<wsdl:portType name="ResourceIdentification" wsrf-rp:ResourceProperties="rvs-mr-ri:ResourceIdentificationProperties "> 
>	</wsdl:portType> 
></wsdl:definitions> 
>   
> 
 
 
--------------020404050508090906030409 
Content-Type: text/html; charset=ISO-8859-1 
Content-Transfer-Encoding: 7bit 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> 
  <title></title> 
</head> 
<body bgcolor="#ffffff" text="#000000"> 
Vladimir,<br> 
<br> 
Don't use backslash in a URI.  It's just treated as a regular 
character, not as "/".<br> 
<blockquote>createURI(<a class="moz-txt-link-rfc2396E" 
 href="file:///C:%5C%5Ctemp%5C%5Cextracted.xsd">"file:///C:\\temp\\extracted.xsd"</a>)<br> 
</blockquote> 
Also, don't use org.eclipse.xsd.impl, i.e., the XSD API implementation 
classes directly either; they are not API and can be changed 
arbitrarily.<br> 
<br> 
The save method of an XSDResourceImpl is like this:<br> 
<blockquote><small>  protected void doSave(OutputStream os, Map 
options) throws IOException<br> 
  {<br> 
    XSDSchema xsdSchema = getSchema();<br> 
    if (xsdSchema != null)<br> 
    {<br> 
      Document document = xsdSchema.<b>getDocument</b>();<br> 
      if (document == null)<br> 
      {<br> 
          xsdSchema.updateDocument();<br> 
          document = xsdSchema.getDocument();<br> 
      }<br> 
  <br> 
      if (xsdSchema.getElement() == null)<br> 
      {<br> 
          xsdSchema.updateElement();<br> 
      }<br> 
  <br> 
      doSerialize(os, document, options == null ? null : 
(String)options.get(XSD_ENCODING));<br> 
    }<br> 
  }</small><br> 
</blockquote> 
Note how it saves the entire document that contains the schema's 
element (because XSDResourceImpl can be used to read a .wsdl directly 
and will wrap all the XSDSchemas in that document, which you can then 
edit, and then save the whole original document again).<br> 
<br> 
You need to extract the schema element into a new Document if you want 
to save just the schema.  Note that 
XSDResourceImpl.serialize(<outputStream>, exElement.getElement()) 
or your save(Node) method  would accomplish the same thing as you are 
doing, but more cheaply, so unless you are actually going to manipulate 
the XSDSchema, this seems an expensive way.  Perhaps just serializing 
the element wouldn't write out all the necessary prefixes declared in a 
outer scopes?<br> 
<br> 
<br> 
Vladimir Isajkin wrote: 
<blockquote cite="middkcqkq$osc$1@news.eclipse.org" type="cite">Hi, 
  <br> 
I extract XSD schema from WSDL as shown in the code below and then try 
to save it. But instead of XSD the complete content of WSDL is saved. 
  <br> 
If I save DOM element using XML Transformer API then I get saved only 
my schema. 
  <br> 
  <br> 
What am I doing wrong? 
  <br> 
  <br> 
Because I intend to use the extracted schema in my next code I need it 
as XSDSchema instance. 
  <br> 
  <br> 
  <br> 
import java.io.File; 
  <br> 
import java.util.Collections; 
  <br> 
import java.util.Iterator; 
  <br> 
import java.util.List; 
  <br> 
  <br> 
import javax.wsdl.Definition; 
  <br> 
import javax.wsdl.WSDLException; 
  <br> 
import javax.wsdl.extensions.UnknownExtensibilityElement; 
  <br> 
import javax.wsdl.factory.WSDLFactory; 
  <br> 
import javax.xml.transform.Result; 
  <br> 
import javax.xml.transform.Source; 
  <br> 
import javax.xml.transform.Transformer; 
  <br> 
import javax.xml.transform.TransformerConfigurationException; 
  <br> 
import javax.xml.transform.TransformerException; 
  <br> 
import javax.xml.transform.TransformerFactory; 
  <br> 
import javax.xml.transform.TransformerFactoryConfigurationError; 
  <br> 
import javax.xml.transform.dom.DOMSource; 
  <br> 
import javax.xml.transform.stream.StreamResult; 
  <br> 
  <br> 
import org.eclipse.emf.common.util.URI; 
  <br> 
import org.eclipse.emf.ecore.resource.Resource; 
  <br> 
import org.eclipse.emf.ecore.resource.ResourceSet; 
  <br> 
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 
  <br> 
import org.eclipse.xsd.XSDFactory; 
  <br> 
import org.eclipse.xsd.XSDSchema; 
  <br> 
import org.eclipse.xsd.impl.XSDSchemaImpl; 
  <br> 
import org.eclipse.xsd.util.XSDResourceImpl; 
  <br> 
import org.w3c.dom.Node; 
  <br> 
  <br> 
  <br> 
public class WsdlHandler { 
  <br> 
    private Definition wsdlDefinition; 
  <br> 
  <br> 
    public static void main(String[] args) { 
  <br> 
          WsdlHandler handler =  new     &nb sp;     &nb sp;     &nb sp;     &nb sp;      
WsdlHandler("ResourceIdentification.wsdl"); 
  <br> 
          handler.extractXSD(); 
  <br> 
    } 
  <br> 
  <br> 
    public WsdlHandler(String wsdlFile) { 
  <br> 
          try { 
  <br> 
              wsdlDefinition = WSDLFactory.newInstance(). 
  <br> 
              newWSDLReader().readWSDL(wsdlFile); 
  <br> 
          } catch (WSDLException exception) { 
  <br> 
              exception.printStackTrace(); 
  <br> 
          } 
  <br> 
    } 
  <br> 
  <br> 
  <br> 
    private void extractXSD() { 
  <br> 
          List exList = wsdlDefinition.getTypes(). 
  <br> 
              getExtensibilityElements(); 
  <br> 
          if (exList != null) { 
  <br> 
              for (Iterator iterator = 
 exList.iterator();    &a mp;nbsp;     &a mp;nbsp;     &a mp;nbsp;     &a mp;nbsp;     &a mp;nbsp;     
iterator.hasNext();) { 
  <br> 
                   UnknownExtensibilityElement exElement 
 =       ;       ;       ;    (UnknownExtensibilityElement) iterator.next(); 
  <br> 
  <br> 
                   XSDSchema schema =XSDSchemaImpl. 
  <br> 
                   createSchema(exElement.getElement()); 
  <br> 
                   //OR 
  <br> 
 //     &nbs p;     &nbs p;    XSDSchema schema = XSDFactory. 
 eINSTANCE.                                       createXSDSchema(); 
  <br> 
 //     &nbs p;     &nbs p;    schema.setElement(exElement); 
  <br> 
                   schema.updateElement(); 
  <br> 
                   schema.update(); 
  <br> 
                   try { 
  <br> 
                        ResourceSet resourceSet = new ResourceSetImpl(); 
  <br> 
                        Resource resource = new XSDResourceImpl(URI 
  <br> 
                               
createURI(<a class="moz-txt-link-rfc2396E" href="file:///C:\\temp\\extracted.xsd">"file:///C:\\temp\\extracted.xsd"</a>)); 
  <br> 
                        resource.getContents().add(schema); 
  <br> 
                        resourceSet.getResources().add(resource); 
  <br> 
                        resource.save(Collections.EMPTY_MAP); 
  <br> 
                   } catch (Exception exception) { 
  <br> 
                        
System.out.println(exception.getLocalizedMessage()); 
  <br> 
                        exception.printStackTrace(); 
  <br> 
                   } 
  <br> 
                   save(exElement.getElement()); 
  <br> 
              } 
  <br> 
          } 
  <br> 
    } 
  <br> 
    private void save(Node node) { 
  <br> 
          try { 
  <br> 
              File outputFile = new File("C:\\temp\\element.xsd"); 
 
  <br> 
              Source source = new DOMSource(node); 
  <br> 
              Result result = new StreamResult(outputFile); 
  <br> 
              Transformer xformer  =       ;       ;       ;       ;      
TransformerFactory.newInstance().newTransformer(); 
  <br> 
              xformer.transform(source, result); 
  <br> 
          } catch (TransformerConfigurationException e) { 
  <br> 
              e.printStackTrace(); 
  <br> 
          } catch (TransformerFactoryConfigurationError e) { 
  <br> 
              e.printStackTrace(); 
  <br> 
          } catch (TransformerException e) { 
  <br> 
              e.printStackTrace(); 
  <br> 
          } 
  <br> 
    } 
  <br> 
} 
  <br> 
  <pre wrap=""> 
<hr size="4" width="90%"> 
<?xml version="1.0" encoding="UTF-8"?> 
<!-- begin_generated_IBM_copyright_prolog		                    --> 
<!--                                                                  --> 
<!-- This is an automatically generated copyright prolog              --> 
<!-- After initializing, DO NOT MODIFY OR MOVE                        --> 
<!--                                                                  --> 
<!--                                                                  --> 
<!--                                                                  --> 
<!-- Licensed Materials - Property of IBM                             --> 
<!-- Restricted Materials of IBM                                      --> 
<!--                                                                  --> 
<!-- Product(s):                                                      --> 
<!--                                                                  --> 
<!-- (C)Copyright IBM Corp.  2005, 2005                               --> 
<!-- All Rights Reserved                                              --> 
<!--                                                                  -->  
<!-- US Government Users Restricted Rights - Use, duplication or      --> 
<!-- disclosure restriced by GSA ADP Schedule Contract with IBM Corp. --> 
<!--                                                                  --> 
<!--                                                                  --> 
<!--                                                                  --> 
<!-- end_generated_IBM_copyright_prolog                               --> 
<wsdl:definitions name="ResourceIdentification"  
targetNamespace=<a class="moz-txt-link-rfc2396E" href=" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.wsdl">" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.wsdl"</a> 
    xmlns:wsdl=<a class="moz-txt-link-rfc2396E" href="http://schemas.xmlsoap.org/wsdl/">"http://schemas.xmlsoap.org/wsdl/"</a>  
    xmlns:xsd=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema">"http://www.w3.org/2001/XMLSchema"</a>  
    xmlns:wsrf-rp=<a class="moz-txt-link-rfc2396E" href=" http://docs.oasis-open.org/wsrf/2004/11/wsrf-WS-ResourceProp erties-1.2-draft-05.xsd">" http://docs.oasis-open.org/wsrf/2004/11/wsrf-WS-ResourceProp erties-1.2-draft-05.xsd"</a> 
    xmlns=<a class="moz-txt-link-rfc2396E" href="http://schemas.xmlsoap.org/wsdl/">"http://schemas.xmlsoap.org/wsdl/"</a>> 
	<wsdl:types> 
		<xsd:schema targetNamespace=<a class="moz-txt-link-rfc2396E" href=" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.wsdl">" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.wsdl"</a> 
            xmlns:rvs-mr-ri=<a class="moz-txt-link-rfc2396E" href=" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.xsd">" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.xsd"</a> 
            xmlns=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema">"http://www.w3.org/2001/XMLSchema"</a> 
            xmlns:xsd=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema">"http://www.w3.org/2001/XMLSchema"</a>> 
        <xsd:import namespace=<a class="moz-txt-link-rfc2396E" href=" http://docs.oasis-open.org/wsrf/2004/11/wsrf-WS-ResourceProp erties-1.2-draft-05.xsd">" http://docs.oasis-open.org/wsrf/2004/11/wsrf-WS-ResourceProp erties-1.2-draft-05.xsd"</a> 
            schemaLocation="WS-ResourceProperties.xsd"/> 
		<xsd:import namespace=<a class="moz-txt-link-rfc2396E" href=" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.xsd">" http://www.ibm.com/xmlns/prod/rvs/2005/05/ResourceIdentifica tion.xsd"</a> 
             schemaLocation="ResourceIdentification.xsd"/> 
		</xsd:schema> 
	</wsdl:types> 
	<wsdl:portType name="ResourceIdentification" wsrf-rp:ResourceProperties="rvs-mr-ri:ResourceIdentificationProperties "> 
	</wsdl:portType> 
</wsdl:definitions> 
  </pre> 
</blockquote> 
<br> 
</body> 
</html> 
 
--------------020404050508090906030409--
 |  
 |  
  |   
Goto Forum:
 
 Current Time: Tue Nov 04 08:39:33 EST 2025 
 Powered by  FUDForum. Page generated in 0.52253 seconds  
 |