My input data looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:queryResponse xmlns:ns1="http://abc.com/">
<ns1:result>
<ns1:records xmlns:ns2="http://def.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:Account">
<ns2:Id>2c</ns2:Id>
<ns2:Number>A722</ns2:AccountNumber>
</ns1:records>
</ns1:result>
</ns1:queryResponse>
I need to write a stylesheet which will create XML of the format:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="Account">
<xsd:sequence>
<xsd:element minOccurs="0" name="Id" type="xsd:string"/>
<xsd:element minOccurs="0" name="Number" type="xsd:string">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The stylesheet should be able to create this XML?
The fields are also dynamically changing, so the stylesheet has to be generic so that it can extract the element name and value from the XML and create the output XML.
Related
I have a schema-aware XSLT transformation that works well. However, the anticipated users of the XSLT will likely not have access to a schema-aware processor. Thus, I'd like to write some code that will convert my schema-aware XSLT into an XSLT that can be used by basic level conforming processors.
For my purposes, the use of schema-aware constructs is limited to matching templates, i.e.
<xsl:template match="element(*, Candidate)">...</>
I'm not expecting this to be a fully automatic process. What I would like is to generate an XSLT that would, for every complexType in an XSD, find its use in a specified surfaced XSD element.
Example code:
<xsd:schema xmlns="example.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="example.xsd" elementFormDefault="qualified" version="0.0">
<xsd:element name="Candidate" type="Candidate"/>
<xsd:complexType name="Candidate">
<xsd:sequence>
<xsd:element name="Code" type="Code" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Name" type="xsd:string" minOccurs="0"/>
<xsd:element name="PartyId" type="Code" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Code">
<xsd:sequence>
<xsd:element name="Value" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The expected output for the element Candidate would be something like this:
<complexType_usages>
<usage complexType="Code" path="Candidate/Code" />
<usage complexType="Code" path="Candidate/Party" />
</complexType_usages>
It might be simplest to work from a validated instance rather than trying to analyse the schema (so long as the instance is sufficiently representative). For every element, output the type annotation (using saxon:type-annotation) and the path, then take the list of type/path pairs, and eliminate duplicates and redundancy.
I have a schema which is something like this:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="1.28.0">
<xsd:complexType name="AccountsReceivableInfo_Type">
<xsd:sequence>
<xsd:element ref="SourceIncomePct" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="DuplicateRecordsPct" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="ID"/>
<xsd:attribute name="Locationref" type="IDref"/>
</xsd:complexType>
<xsd:complexType name="AccountsDeleteInfo_Type">
<xsd:sequence>
<xsd:element ref="SourceIncomePct" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="DuplicateRecordsPct" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="ID"/>
<xsd:attribute name="Locationref" type="IDref"/>
</xsd:complexType>
and I am trying to modify it and write an XSLT script to:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="1.28.0">
<xsd:complexType name="AccountsReceivableInfo_Type">
<xsd:sequence>
<!--<xsd:element ref="SourceIncomePct" minOccurs="0" maxOccurs="1"/>-->
<xsd:element ref="DuplicateRecordsPct" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="ID"/>
<xsd:attribute name="Locationref" type="IDref"/>
</xsd:complexType>
<xsd:complexType name="AccountsDeleteInfo_Type">
<xsd:sequence>
<xsd:element ref="SourceIncomePct" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="DuplicateRecordsPct" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="ID"/>
<xsd:attribute name="Locationref" type="IDref"/>
</xsd:complexType>
XSLT script which i wrote so far is
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="xsd:complexType[#name='AccountsReceivableInfo_Type']
/xsd:element[#ref ='SourceIncomePct']">
<xsl:text disable-output-escaping="yes"><!--</xsl:text>
<xsl:sequence select="."/>
<xsl:text disable-output-escaping="yes">--></xsl:text>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy><xsl:apply-templates select="#*|node()"/></xsl:copy>
</xsl:template>
</xsl:transform>
What I am trying to do is to modify a big schema like above and automate it and need to read a particular element within a particular complextype parent and modify only that instance like commenting it out.
If you want to put the element into a comment consider to move to XSLT 3.0 and use
<xsl:template match="xsd:complexType[#name='AccountsReceivableInfo_Type']/xsd:sequence/xsd:element[#ref ='SourceIncomePct']">
<xsl:comment select="serialize(.)"/>
</xsl:template>
I'm making a contract first webservice so my first xds(MensajeDetails.xds) is:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://webservices.samples.blog.com" targetNamespace="http://webservices.samples.blog.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Mensaje" type="Mensaje"/>
<xs:complexType name="Mensaje">
<xs:sequence>
<xs:element name="IdMensajesEnviados" type="xs:long"/>
<xs:element name="CodigoEstatus" type="xs:int"/>
<xs:element name="DescripcionEstatus" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
and in my 2nd xds (MensajeDetailsServiceOperation.xds) I´ve:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://com/blog/samples/webservices/mensajeservice" xmlns:account="http://webservices.samples.blog.com" targetNamespace="http://com/blog/samples/webservices/mensajeservice" elementFormDefault="qualified">
<xsd:import namespace="http://webservices.samples.blog.com" schemaLocation="MensajeDetails.xsd"/>
<xsd:element name="MensajeDetailsRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="IdUsuario" type="xsd:long"/>
<xsd:element name="Token" type="xsd:string"/>
<xsd:element name="IdServicio" type="xsd:int"/>
<xsd:element name="Archivo" type="xsd:byte"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="MensajeDetailsResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MensajeDetails" type="mensaje:Mensaje"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
So eclipse is telling me that in my 2nd xds
s4s-att-invalid-value: Invalid attribute value for 'type' in element 'element'.
Recorded reason: UndeclaredPrefix: Cannot resolve 'mensaje:Mensaje' as a QName: the prefix 'mensaje' is not declared." in the line:
<xsd:element name="MensajeDetails" type="mensaje:Mensaje"/>
What am I doing wrong?
You have imported the namespace into your schema and have declared a namespace prefix for it, in your schema declaration your have xmlns:account="http://webservices.samples.blog.com", this binds the prefix "account" to your imported namespace.
So, either change your element to account:Mensaje or change the prefix to mensaje
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://com/blog/samples/webservices/mensajeservice"
xmlns:mensaje="http://webservices.samples.blog.com"
targetNamespace="http://com/blog/samples/webservices/mensajeservice"
elementFormDefault="qualified">
This is my current configuration:
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://stock.com/schemas/services/stock"
xmlns:tns="http://stock.com/schemas/services/stock"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" targetNamespace="http://stock.com/schemas/services/stock">
<xsd:element name="Stock">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="ticker" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="quotes" nillable="true" type="Quote"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="Quote">
........
</xsd:complexType>
.......
<xsd:element name="gethighBetaStockResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="stock" ref="Stock" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
WSDL
<?xml version="1.0" encoding="UTF-8"?><definitions targetNamespace="http://stock.com/schemas/services/stock/wsdl"
.....xmlns:external="http://stock.com/schemas/services/stock"
<import namespace="http://stock.com/schemas/services/stock" location="Stock.xsd" />
<message name="getStockQuoteResp">
<part name="parameters" element="external:getStockQuoteResponse" />
</message>
However,the moment ref="Stock" is changed to type="Stock",the wsdl2java starts giving
Type {http://stock.com/schemas/services/stock}Stock is referenced but not defined.
Somehow it seems a clash between wsdl and xsd imports - but I just cant resolve it.Help is appreciated.
You have a couple of problems here.
First, the XSD has an issue where an element is both named or referenced; in your case should be referenced.
Change:
<xsd:element name="stock" ref="Stock" minOccurs="1" maxOccurs="unbounded"/>
To:
<xsd:element name="stock" type="Stock" minOccurs="1" maxOccurs="unbounded"/>
And:
Remove the declaration of the global element Stock
Create a complex type declaration for a type named Stock
So:
<xsd:element name="Stock">
<xsd:complexType>
To:
<xsd:complexType name="Stock">
Make sure you fix the xml closing tags.
The second problem is that the correct way to reference an external XSD is to use XSD schema with import/include within a wsdl:types element. wsdl:import is reserved to referencing other WSDL files. More information is available by going through the WS-I specification, section WSDL and Schema Import. Based on WS-I, your case would be:
INCORRECT: (the way you showed it)
<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="http://stock.com/schemas/services/stock/wsdl"
.....xmlns:external="http://stock.com/schemas/services/stock"
<import namespace="http://stock.com/schemas/services/stock" location="Stock.xsd" />
<message name="getStockQuoteResp">
<part name="parameters" element="external:getStockQuoteResponse" />
</message>
</definitions>
CORRECT:
<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="http://stock.com/schemas/services/stock/wsdl"
.....xmlns:external="http://stock.com/schemas/services/stock"
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<import namespace="http://stock.com/schemas/services/stock" schemaLocation="Stock.xsd" />
</schema>
</types>
<message name="getStockQuoteResp">
<part name="parameters" element="external:getStockQuoteResponse" />
</message>
</definitions>
SOME processors may support both syntaxes. The XSD you put out shows issues, make sure you first validate the XSD.
It would be better if you go the WS-I way when it comes to WSDL authoring.
Other issues may be related to the use of relative vs. absolute URIs in locating external content.
import vs. include
The primary purpose of an import is to import a namespace. A more common use of the XSD import statement is to import a namespace which appears in another file. You might be gathering the namespace information from the file, but don't forget that it's the namespace that you're importing, not the file (don't confuse an import statement with an include statement).
Another area of confusion is how to specify the location or path of the included .xsd file: An XSD import statement has an optional attribute named schemaLocation but it is not necessary if the namespace of the import statement is at the same location (in the same file) as the import statement itself.
When you do chose to use an external .xsd file for your WSDL, the schemaLocation attribute becomes necessary. Be very sure that the namespace you use in the import statement is the same as the targetNamespace of the schema you are importing. That is, all 3 occurrences must be identical:
WSDL:
xs:import namespace="urn:listing3" schemaLocation="listing3.xsd"/>
XSD:
<xsd:schema targetNamespace="urn:listing3"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Another approach to letting know the WSDL about the XSD is through Maven's pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-sources-xmlbeans</id>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
<version>2.3.3</version>
<inherited>true</inherited>
<configuration>
<schemaDirectory>${basedir}/src/main/xsd</schemaDirectory>
</configuration>
</plugin>
You can read more on this in this great IBM article. It has typos such as xsd:import instead of xs:import but otherwise it's fine.
I have the following xsd
<xsd:complexType name="myID">
<xsd:choice>
<xsd:element name="testID" type="priv:testID"/>
<xsd:sequence>
<xsd:element name="newID" type="priv:newID"/>
<xsd:element name="testID" type="priv:testID" minOccurs="0"/>
</xsd:sequence>
</xsd:choice>
</xsd:complexType>
Everything is under priv namespace. The problem is that it looks like that myID is a union. It might be a testID or a sequence with newID and testID. When I compile it with wsdl2h from gsoap I am taking the message:
Note: <xs:choice> with embedded
<xs:sequence> or <xs:group>
prevents the use of a union
Is the above XSD correct?
In general the XML type myID can be declared as you described. The conflict exist probably in connection with your definition of the types priv:testID and priv:testID which definition you not included. For example the schema
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema targetNamespace="http://www.ok-soft-gmbh.com/xml/xsd/1.0/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns:priv="http://www.ok-soft-gmbh.com/xml/xsd/1.0/XMLSchema.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
<xsd:simpleType name="testID">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="newID">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:complexType name="myID">
<xsd:choice>
<xsd:element name="testID" type="priv:testID"/>
<xsd:sequence>
<xsd:element name="newID" type="priv:newID"/>
<xsd:element name="testID" type="priv:testID" minOccurs="0"/>
</xsd:sequence>
</xsd:choice>
</xsd:complexType>
<xsd:element name="root" type="priv:myID"/>
</xsd:schema>
will be correct. So if an error exist, it is not in the part which you posted.