At least 1 input required in XML - java

This is my input structure to the API,'search serves'
<structure name="SearchServesInput" >
<member name="ClientUser" target="Username" />
<member name="OwnerUser" target="Username" />
<member name="ServiceName" target="String" />
<member name="EnableSearch" target="Boolean" />
<member name="PanControls" target="PanControls" /> </structure>
At least 1 of the first three fields is required. How do I implement that?

You will have to declare a schema, here is a small example:
<?xml version="1.0"?>
<xs:schema>
<xs:attribute name="test" type="xs:string" use="required"/>
</xs:schema>
You can check here some tutorials.

Related

JAXB - When property already defined and using bindings it's not working with *complexContent*

Using JAXB to generate Java classes.
I'm using the following XSD scheme.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://schemas.microsoft.com/developer/msbuild/2003" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msb="http://schemas.microsoft.com/developer/msbuild/2003" elementFormDefault="qualified">
<!-- Working -->
<xs:complexType name="MetaTypeSimpleContent">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="Name" />
<xs:attribute type="xs:string" name="Scheme" />
<xs:attribute type="xs:string" name="Value" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- Not Working -->
<xs:complexType name="MetaTypeComplexContent">
<xs:complexContent>
<xs:extension base="msb:BaseType">
<xs:attribute name="MyName" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="BaseType">
<xs:attribute name="MyName" type="xs:string" />
</xs:complexType>
</xs:schema>
..and the following Binding definition
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="Test.xsd" version="1.0">
<!-- Customise the package name -->
<schemaBindings>
<package name="com.example.schema" />
</schemaBindings>
<!-- Working -->
<bindings node="//xs:complexType[#name='MetaTypeSimpleContent']">
<bindings node=".//xs:attribute[#name='Value']">
<property name="ValueAttribute" />
</bindings>
</bindings>
<!-- Not Working -->
<bindings node="//xs:complexType[#name='MetaTypeComplexContent']/xs:complexContent/xs:extension/xs:attribute[#name='MyName']">
<property name="MyName3" />
</bindings>
</bindings>
</bindings>
The binding for the complextType MetaTypeSimpleContent with the simpleContent works fine. But the binding for the complexType MetaTypeComplexContent with the complexContent doesn't. Why?
I got the following Error:
[ERROR] ct-props-correct.4: Error with type 'MetaTypeComplexContent'. Duplicate attribute usages were specified with the same name and target namespace. The name of the duplicate attribute is 'MyName'.
Line 19 of file: / D: /temp/Stackoverflow/Test.xsd
A schema could not be parsed.
The error message seems to be pointing out the problem quite clearly:
[ERROR] ct-props-correct.4: Error with type 'MetaTypeComplexContent'. Duplicate attribute usages were specified with the same name and target namespace. The name of the duplicate attribute is 'MyName'.
When extending a complex type, you should not re-declare the inherited content. Maybe you intended to add a new attribute in the extended complex type, but you forgot to change the name?
To fix the error, do one of the following:
delete the complex type extension
change the name or namespace of the attribute in the complex type extension

One XSD for both whole document and fragment?

I wish to create a grammar able to validate both a full XML document and a fragment of it.
I have a set of documents aggregate in a “batch”. Each document has a set of meta-data:
<batch>
<document>
<metadata1 />
<metadata2 />
<metadata3 />
</document>
<document>
<metadata1 />
<metadata2 />
<metadata3 />
</document>
</batch>
My SpringBatch process splits the batch in documents (with StaxEventItemReader)
I wish to validate a sub XML representing a single document:
<document>
<metadata1 />
<metadata2 />
<metadata3 />
</document>
I read here that I can’t use partial XSD to validate XML.
However, is there is a way, while avoiding duplication, to validate with two XSDs, where one would validate the fragment, and the other would validate the batch?
You can achieve your goal with a single XSD by specifying multiple possible root elements:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="batch">
<xs:complexType>
<xs:sequence>
<xs:element ref="document" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="document">
<xs:complexType>
<xs:sequence>
<xs:element name="metadata1" type="xs:string" />
<xs:element name="metadata2" type="xs:string" />
<xs:element name="metadata3" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This way documents may have either a batch or a document root element, and there is no definition duplication.

already defined error when converting xsd to java

I want to translate a complicated set of xsd files into Java/Hibernate entities. Towards that end,
I downloaded version 2.2.7 of JAXB from this link. I unzipped the file, opened cmd.exe, navigated to the directory of the create-marshal sample, ran ant compile to confirm everything works, then ran ant clean to eliminate the results to return everything to starting conditions.
The problem came when I tried to use a different xsd file as the input for the create-marshal sample. Specifically, I am getting an error when the xsd file distinguishes between data structures by changing the value of an attribute of the same tag type. The create-marshal sample gives the following error in that case:
[ERROR] 'POCD_MT000040.InfrastructureRoot.typeId' is already defined
[xjc] line 54 of file:/C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd
The error repeats again and again, once for every time that the value of the attribute is re-assigned for a different data structure definition. How can I resolve this error?
Here are the first few iterations of the data structure in the xsd file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema targetNamespace="urn:the-publisher:v3"
xmlns:mif="urn:the-publisher:v3/mif"
xmlns="urn:the-publisher:v3"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns:sdtc="urn:the-publisher:sdtc">
<xs:annotation>
<xs:documentation>Manually edited to add Schema Extensions, July 2012</xs:documentation>
<xs:documentation>Generated using schema builder version 2.0. Stylesheets: RoseTreeToMIFStaticModel.xsl version: 1.1 StaticMifToXsd.xsl version 2.0</xs:documentation>
</xs:annotation>
<xs:complexType name="POCD_MT000040.InfrastructureRoot.typeId">
<xs:complexContent>
<xs:restriction base="II">
<xs:attribute name="root" type="uid"
use="required"
fixed="2.16.840.1.113883.1.3"/>
<xs:attribute name="extension" type="st"
use="required"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="POCD_MT000040.Act">
<xs:sequence>
<xs:element name="realmCode" type="CS"
minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="typeId"
type="POCD_MT000040.InfrastructureRoot.typeId"
minOccurs="0"/>
<xs:element name="templateId" type="II"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="id" type="II" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="code" type="CD"/>
<xs:element name="text" type="ED" minOccurs="0"/>
<xs:element name="statusCode" type="CS"
minOccurs="0"/>
<xs:element name="effectiveTime" type="IVL_TS"
minOccurs="0"/>
<xs:element name="priorityCode" type="CE"
minOccurs="0"/>
<xs:element name="languageCode" type="CS"
minOccurs="0"/>
<xs:element name="subject"
type="POCD_MT000040.Subject" minOccurs="0"/>
<xs:element name="specimen"
type="POCD_MT000040.Specimen" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="performer"
type="POCD_MT000040.Performer2" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="author"
type="POCD_MT000040.Author" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="informant"
type="POCD_MT000040.Informant12"
minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="participant"
type="POCD_MT000040.Participant2"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="entryRelationship"
type="POCD_MT000040.EntryRelationship"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="reference"
type="POCD_MT000040.Reference"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="precondition"
type="POCD_MT000040.Precondition"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="nullFlavor"
type="NullFlavor" use="optional"/>
<xs:attribute name="classCode"
type="x_ActClassDocumentEntryAct" use="required"/>
<xs:attribute name="moodCode" type="x_DocumentActMood"
use="required"/>
<xs:attribute name="negationInd" type="bl"
use="optional"/>
</xs:complexType>
<xs:complexType name="POCD_MT000040.AssignedAuthor">
<xs:sequence>
<xs:element name="realmCode" type="CS"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="typeId"
type="POCD_MT000040.InfrastructureRoot.typeId"
minOccurs="0"/>
<xs:element name="templateId" type="II"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="id" type="II"
maxOccurs="unbounded"/>
<xs:element name="code" type="CE" minOccurs="0"/>
<xs:element name="addr" type="AD"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="telecom" type="TEL"
minOccurs="0" maxOccurs="unbounded"/>
<xs:choice>
<xs:element name="assignedPerson"
type="POCD_MT000040.Person" minOccurs="0"/>
<xs:element name="assignedAuthoringDevice"
type="POCD_MT000040.AuthoringDevice"
minOccurs="0"/>
</xs:choice>
<xs:element name="representedOrganization"
type="POCD_MT000040.Organization" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="nullFlavor"
type="NullFlavor" use="optional"/>
<xs:attribute name="classCode"
type="RoleClassAssignedEntity"
use="optional" fixed="ASSIGNED"/>
</xs:complexType>
Here are the corresponding first few iterations of the error message:
Build sequence for target(s) `compile' is [compile]
Complete build sequence is [compile, javadoc, clean, run, ]
compile:
[echo] Compiling the schema...
[mkdir] Created dir: C:\Temp\jaxb\apps\create-marshal\gen-src
[mkdir] Created dir: C:\Temp\jaxb\apps\create-marshal\gen-src\primer\po
[xjc] build id of XJC is 2.2.7
[xjc] Checking timestamp of C:\Temp\jaxb\apps\create-marshal\POCD_MT000040_SDTC.xsd
[xjc] the last modified time of the inputs is 1409957672397
[xjc] the last modified time of the outputs is -9223372036854775808
[xjc] Compiling file:/C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd
[xjc] [ERROR] 'POCD_MT000040.InfrastructureRoot.typeId' is already defined
[xjc] line 54 of file:/C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd
[xjc]
[xjc] [ERROR] (related to above error) the first definition appears here
[xjc] line 46 of file:/C:/Temp/jaxb/apps/create-marshal/infrastructure/cda/POCD_MT000040_SDTC.xsd
[xjc]
[xjc] [ERROR] 'POCD_MT000040.Act' is already defined
[xjc] line 81 of file:/C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd
[xjc]
.....the same error repeats many more times, once for each repetition of xs:complexType
[xjc] failure in the XJC task. Use the Ant -verbose switch for more details
compile: duration 2 seconds
BUILD FAILED
C:\Temp\jaxb\apps\create-marshal\build.xml:29: unable to parse the schema. Error messages should have been provided
at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:520)
at com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:457)
at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:380)
at com.sun.istack.tools.ProtectedTask.execute(ProtectedTask.java:103)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Here is the ant build file:
<?xml version="1.0" standalone="yes"?>
<!-- Copyright 2004 Sun Microsystems, Inc. All rights reserved. -->
<project basedir="." default="run">
<description>This sample application demonstrates how to use the ObjectFactory class to create a Java content tree from scratch and marshal it to XML data. It also demonstrates how to add content to a JAXB List property.</description>
<record name="build.log" loglevel="verbose" action="start"/>
<property name="jaxb.home" value="../.." />
<path id="classpath">
<pathelement path="src" />
<pathelement path="classes" />
<pathelement path="schemas" />
<fileset dir="${jaxb.home}" includes="lib/*.jar" />
</path>
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
<classpath refid="classpath" />
</taskdef>
<!--compile Java source files-->
<target name="compile" description="Compile all Java source files">
<echo message="Compiling the schema..." />
<mkdir dir="gen-src" />
<mkdir dir="gen-src/primer/po" />
<xjc schema="POCD_MT000040_SDTC.xsd" package="primer.po" destdir="gen-src">
<produces dir="gen-src/primer/po" includes="**/*.java" />
</xjc>
<echo message="Compiling the java source files..." />
<mkdir dir="classes" />
<javac destdir="classes" debug="on">
<src path="src" />
<src path="gen-src" />
<classpath refid="classpath" />
</javac>
</target>
<target name="run" depends="compile" description="Run the sample app">
<echo message="Running the sample application..." />
<java classname="Main" fork="true">
<classpath refid="classpath" />
</java>
</target>
<target name="javadoc" description="Generates javadoc" depends="compile">
<echo message="Generating javadoc..." />
<mkdir dir="docs/api" />
<javadoc sourcepath="gen-src" destdir="docs/api" windowtitle="create-marshal (formerly SampleApp3)" useexternalfile="yes">
<fileset dir="." includes="gen-src/**/*.java" excludes="**/impl/**/*.java" />
</javadoc>
</target>
<target name="clean" description="Deletes all the generated artifacts.">
<delete dir="docs/api" />
<delete dir="gen-src" />
<delete dir="schemas" />
<delete dir="classes" />
</target>
</project>
The error you are reporting indicates that, somehow, ant has been instructed to read the same XML Schema definitions twice. Indeed, the error message says that one definition is in
C:\Temp\jaxb\apps\create-marshal\POCD_MT000040_SDTC.xsd
and the other one was found in
...\create-marshal\infrastructure\cda\POCD_MT000040_SDTC.xsd
You should clean up the file tree below create-marshal. You need one of these two, and, relatived to this file, the ones included by it, e.g.:
..\coreschemas\datatypes.xsd
..\coreschemas\voc.xsd
..\coreschemas\NarrativeBlock.xsd
Finally, datatypes-base.xsd must be in the same folder as these three, as it is included by datatypes.xsd.
But when you have tidied this up, you'll run into another problem, at least with the JAXB that comes with JDK 1.8. This is a bug in xjc, triggered by the (unintentional) duplication of a field "id" in a number of complex types, e.g., here:
<xs:complexType name="POCD_MT000040.ObservationMedia">
...
<xs:element name="id" type="II" minOccurs="0" maxOccurs="unbounded"/>
...
<xs:attribute name="ID" type="xs:ID"/>
The mapping of XML Schema names to Java names is defined in the JAXB spec, and, no matter how a clash is produced, this should result in an error message (not a stack dump, as it happens).
You have to handle this problem (bug or no bug) using customization with a JAXB binding file. Here's one (let'S call it rename.xjb), renaming the field matching #ID in POCD_MT000040.ObservationMedia to `xsid
FIXES ALL NAME CLASHES id vs. ID
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="./POCD_MT000040.xsd" node="/xs:schema">
<bindings node="//xs:complexType[#name='POCD_MT000040.ObservationMedia']/xs:attribute[#name='ID']">
<property name="xsid"/>
</bindings>
</bindings>
<bindings schemaLocation="./POCD_MT000040.xsd" node="/xs:schema">
<bindings node="//xs:complexType[#name='POCD_MT000040.RegionOfInterest']/xs:attribute[#name='ID']">
<property name="xsid"/>
</bindings>
</bindings>
<bindings schemaLocation="./POCD_MT000040.xsd" node="/xs:schema">
<bindings node="//xs:complexType[#name='POCD_MT000040.Section']/xs:attribute[#name='ID']">
<property name="xsid"/>
</bindings>
</bindings>
</bindings>
You have to add the bindings file to the xjc invocation. In ant, this would be the #binding of <xjc>:
<xjc schema="POCD_MT000040_SDTC.xsd" binding="rename.xjb" ...

What is the simplest way to transform an xml to another one?

I have an xml document which looks like this:
<document>
<primitive name="password" type="xs:string" path="" mandatory="false" />
<structure name="username" mandatory="true">
<primitive name="first-name" type="xs:string" path="" mandatory="true" />
<primitive name="last-name" type="xs:string" path="" mandatory="true" />
</structure>
<list name="addresses" path="" mandatory="true">
<structure name="address" mandatory="true">
<primitive name="country" type="xs:string" path="" mandatory="true" default-value="HU"/>
<primitive name="po-box" type="xs:string" path="" mandatory="true" />
</structure>
</list>
<list name="owned-phones" path="" mandatory="true">
<structure name="phone" mandatory="true">
<primitive name="number" type="xs:int" path="" mandatory="true" />
<primitive name="device-name" type="xs:string" path="" mandatory="true" />
<list name="similar-devices" path="" mandatory="true">
<structure name="device" mandatory="true">
<primitive name="device-name" type="xs:string" path="" mandatory="true" />
</structure>
</list>
</structure>
</list>
<list name="aliases" path="" mandatory="true">
<primitive name="alias" type="xs:string" path="" mandatory="true" />
</list>
<template name="tou">
<![CDATA[
wombat
]]>
</template>
This describes rules for a data structure which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<document>
<password>ajshakjsdh</password>
<username>
<first-name>Kevin</first-name>
<last-name>Smith</last-name>
</username>
<addresses>
<address>
<country>HU</country>
<po-box>12234</po-box>
</address>
<address>
<country>US</country>
<po-box>666</po-box>
</address>
</addresses>
<owned-phones>
<phone>
<number>2431241</number>
<device-name>Nokia</device-name>
<similar-devices>
<device>
<device-name>Windozfon</device-name>
</device>
</similar-devices>
</phone>
<phone>
<number>68741</number>
<device-name>Samsung</device-name>
<similar-devices>
<device>
<device-name>Android</device-name>
</device>
<device>
<device-name>Blackberry</device-name>
</device>
</similar-devices>
</phone>
</owned-phones>
<aliases>
<alias>Alias1</alias>
<alias>Alias2</alias>
</aliases>
<!-- tou is missing but not mandatory -->
</document>
I have a set of transformation rules which can be used to convert the first xml file to an xsd document which can be used to validate the data structure (second code block):
<primitive name="$primitive_name" type="$primitive_type" mandatory="$primitive_mandatory" />
transforms to
<xs:element name="$primitive_name" type="$primitive_type" minOccurs="$primitive_mandatory ? 1 : 0" maxOccurs="1" />
<structure name="$structure_name" mandatory="$structure_mandatory">
<!-- anything inside recursively transformed -->
</structure>
transforms to
<xs:element name="$structure_name" minOccurs="$structure_mandatory ? 1 : 0" maxOccurs="1">
<xs:complexType>
<xs:all>
<!-- anything inside recursively transformed -->
</xs:all>
</xs:complexType>
</xs:element>
<list name="$list_name" mandatory="$list_mandatory">
<!-- anything inside recursively transformed -->
</list>
transforms to
<xs:element name="$list_name" minOccurs="$list_mandatory ? 1 : 0">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<!-- anything inside recursively transformed -->
</xs:sequence>
</xs:complexType>
</xs:element>
My question is what tool should I use for these simple transformations? I've heard that XSLT is the way to go here. I did a little research about it and it seems overkill to me. At least I was not able to find the correct transformation rules in XSLT. Can you provide a few pointers where should I start?
XSLT is very a good solution for your problem. You can easily map your structure to individual templates which will be called recursively.
Here is an example of a XSLT 2.0 stylesheet with templates for just the the examples you provided. It should provide a good starting point.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="document">
<xs:schema elementFormDefault="qualified">
<xsl:apply-templates/>
</xs:schema>
</xsl:template>
<xsl:template match="primitive">
<xs:element name="{#name}" type="{#type}" minOccurs="{if(#mandatory eq 'true') then 1 else 0}" maxOccurs="1" />
</xsl:template>
<xsl:template match="structure">
<xs:element name="{#name}" minOccurs="{if(#mandatory eq 'true') then 1 else 0}" maxOccurs="1">
<xs:complexType>
<xs:all>
<xsl:apply-templates/>
</xs:all>
</xs:complexType>
</xs:element>
</xsl:template>
<xsl:template match="list">
<xs:element name="{#name}" minOccurs="{if(#mandatory eq 'true') then 1 else 0}">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xsl:apply-templates/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsl:template>
</xsl:stylesheet>
There are many ways to achieve the same results. Look for a good XSLT tutorial that makes efficient use of XSLT templates. Avoid the ones that focus on single-template solutions using for-each. You can achieve everything you need using templates (and not using <xsl:for-each> which will make it excessively more complicated).
In case your support is limited to XSLT 1.0, you won't be able to use the (XPath 2.0) code in the minOccurs attributes, but you can generate the attribute with a nested <xsl:attribute> and use <xsl:choose> to calculate its contents.
The stylesheet above could be rewritten in XSLT 1.0 as below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="document">
<xs:schema elementFormDefault="qualified">
<xsl:apply-templates/>
</xs:schema>
</xsl:template>
<xsl:template match="primitive">
<xs:element name="{#name}" type="{#type}" maxOccurs="1">
<xsl:attribute name="minOccurs">
<xsl:choose>
<xsl:when test="#mandatory = 'true'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xs:element>
</xsl:template>
<xsl:template match="structure">
<xs:element name="{#name}" maxOccurs="1">
<xsl:attribute name="minOccurs">
<xsl:choose>
<xsl:when test="#mandatory = 'true'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xs:complexType>
<xs:all>
<xsl:apply-templates/>
</xs:all>
</xs:complexType>
</xs:element>
</xsl:template>
<xsl:template match="list">
<xs:element name="{#name}">
<xsl:attribute name="minOccurs">
<xsl:choose>
<xsl:when test="#mandatory = 'true'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xsl:apply-templates/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsl:template>
</xsl:stylesheet>
you are right, XSLT is the tool to use.
For your case it should be quite fast to create your xsl file as you don't have to much statements to convert.
You will need XSL and XPATH (Xml query language) to do the job
To start with, I suggest to go here http://www.w3schools.com/xsl/
and you will find the reference doc to XSLT there: http://www.w3.org/TR/xslt
For XPATH: http://www.w3.org/TR/xpath/ and http://www.w3schools.com/XPath/
Hope it will help.
Enjoy ;)
Yann

My XSD does not validate the XML despite proper namespace declarations

The XSD <schema> tag:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cmu.edu/ns/blank"
targetNamespace="http://www.cmu.edu/ns/blank"
elementFormDefault="qualified">
The root of the XML, <people> tag.
<people
xmlns="http://www.cmu.edu/ns/blank"
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd">
How ever I get en error that:
cvc-elt.1.a: Cannot find the declaration of element 'people'
I know it has something to do with namespaces but I can not figure out what.
Please help
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!-- <!DOCTYPE people SYSTEM "validator.dtd"> -->
<people
xmlns="http://www.cmu.edu/ns/blank"
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd">
<student>
<name>John</name>
<course>Computer Technology</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
<student>
<name>Foo</name>
<course>Industrial Electronics</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
</people>
XSD
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cmu.edu/ns/blank"
targetNamespace="http://www.cmu.edu/ns/blank"
elementFormDefault="qualified">
<xs:element name="people">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="course" type="xs:string" />
<xs:element name="semester">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1" />
<xs:enumeration value="2" />
<xs:enumeration value="3" />
<xs:enumeration value="4" />
<xs:enumeration value="5" />
<xs:enumeration value="6" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="scheme">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value = "E|C" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The only thing that's not working is the schema location attribute's namespace. The XML Schema Instance namespace is:
http://www.w3.org/2001/XMLSchema-instance
instead of:
http://www.w3c.org/2001/XMLSchema-instance (you put a c in w3c)
For the given XML, the error is
The 'http://www.w3c.org/2001/XMLSchema-instance:schemaLocation' attribute is not declared.

Categories