The prefix "xsd" for element "xsd:element" is not bound. [11] - java

So I have a task to complete for Friday involving creating a shares system and I'm completely lost in Netbeans. I've used it once before and did reasonably well but I've been stuck on this same issue for 2 hours now.
I've obviously gotta create the XML and accompanying schema for this project but started from a blank XSD now, I can't validate the code and also have troubles to create the constrained XML document from the XSD as there isn't a primary Element. If anyone could point me in the right direction that would be a great help. Me and Netbeans do not seem to get along. The XSD is as follows and yes, it's probably something completely obvious.
<xs:schema version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="shares">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CompanyName" type="xsd:string"/>
<xsd:element name="CompanySymbol" type="xsd:string"/>
<xsd:element name="AvailableShares" type="xsd:string"/>
<xsd:element name="Updated" type="xsd:date"/>
<xsd:complexType>
<xsd:element name="Currency" type="xsd:float"/>
<xsd:element name="Value" type="xsd:float"/>
</xsd:complexType>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

The xml only declared the namespace xmlns:xs="http://www.w3.org/2001/XMLSchema" but uses instead the prefix xsd.
Also the closing </xsd:schema> is missing.
Here is the fixed xml:
<xsd:schema version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="shares">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CompanyName" type="xsd:string"/>
<xsd:element name="CompanySymbol" type="xsd:string"/>
<xsd:element name="AvailableShares" type="xsd:string"/>
<xsd:element name="Updated" type="xsd:date"/>
<xsd:complexType>
<xsd:element name="Currency" type="xsd:float"/>
<xsd:element name="Value" type="xsd:float"/>
</xsd:complexType>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Related

cvc-elt.1: Cannot find the declaration of element 'change-set'

I'm trying to use some open-source software for RFID services (GitHub: fosstrak capturing application) from a few years ago and there's this error that I can't fix.
(null: 3, 230): cvc-elt.1: Cannot find the declaration of element 'change-set'.
I'm using Docker to containerise the application running Tomcat7 with Java8.
The XML looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<change-set xmlns="http://drools.org/drools-5.0/change-set"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://drools.org/drools-5.0/change-set.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd">
<add>
<resource source="classpath:drools/SimpleEPCISDocument.drl" type="DRL" />
</add>
</change-set>
And the XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://drools.org/drools-5.0/change-set"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xmlns:change-set="http://drools.org/drools-5.0/change-set">
<xsd:import namespace="http://www.w3.org/2001/XMLSchema-instance"/>
<xsd:element name="change-set">
<xsd:complexType>
<xsd:choice>
<xsd:element ref="change-set:add"/>
<xsd:element ref="change-set:remove"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<xsd:element name="add">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" ref="change-set:resource"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="remove">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" ref="change-set:resource"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="resource">
<xsd:complexType>
<xsd:sequence>
<xsd:any minOccurs="0"/>
<xsd:element minOccurs="0" ref="change-set:decisiontable-conf"/>
</xsd:sequence>
<!-- URL to the resource, can be file based -->
<xsd:attribute name="source" use="required" type="xsd:anyURI"/>
<!-- for example, DRL, or PKG -->
<xsd:attribute name="type" use="required" type="xsd:string"/>
<xsd:attribute name="basicAuthentication" type="xsd:string"/>
<xsd:attribute name="username" type="xsd:string"/>
<xsd:attribute name="password" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="decisiontable-conf">
<xsd:complexType>
<xsd:attribute name="input-type" use="required" type="xsd:NCName"/>
<xsd:attribute name="worksheet-name" use="required" type="xsd:NCName"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Change
xs:schemaLocation="http://drools.org/drools-5.0/change-set.xsd [...]
to
xs:schemaLocation="http://drools.org/drools-5.0/change-set [...]
because schemaLocation must be a series of pairs of namespace URIs and XSD locations. In this case, the namespace URI is http://drools.org/drools-5.0/change-set, which matches the namespace of the root element in your XML and the target namespace of your XSD.
See also
How to reference a local XML Schema file correctly?
You'll also have to deal with a subsequent Unique Particle Attribute issue, but that's a separate matter deserving its own question if you're unable to resolve. Start with XSD validation - ANY in SEQUENCE ("Unique Particle Attribution").

Error while trying to create Java Class out of .xsd file

I have two XMLs and i want convert them into java classes so i went to this page where i can generate .xsd files. I'm trying to make two Java Class out of them in Eclipse and I'm getting the same error:
parsing a schema...
[ERROR] Content is not allowed in prolog.
line 1 of file:/(...)/queryClass.xsd
Failed to parse a schema.
These are my .xsd files:
First:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="dao" type="daoType" />
<xsd:complexType name="daoType">
<xsd:sequence>
<xsd:element name="query" type="queryType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="queryType">
<xsd:sequence>
<xsd:element name="arguments" type="argumentsType" />
<xsd:element name="statement" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="argumentsType">
<xsd:sequence>
<xsd:element name="argument" type="argumentType" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="argumentType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
Second:
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="properties" type="propertiesType" />
<xsd:complexType name="propertiesType">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="entry" type="entryType" />
</xsd:sequence>
<xsd:attribute name="version" type="xsd:decimal" />
</xsd:complexType>
<xsd:complexType name="entryType">
<xsd:attribute name="key" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
It turned out that the XSD files were wrong. I used another XML-XSD converter and it worked perfectly fine.

Not able to validate XSD for an XML with a namespace

I am not able to validate the XML using the below XSD,
<myTest>
<standardHeader xmlns="http://tow.gow.ho.com/2009/08/StandardHeader/">
<data>
<LEVELDATA>D2D</LEVELDATA>
</data>
</standardHeader>
</myTest>
XSD Data
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="myTest">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="standardHeader" minOccurs="0">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="data" minOccurs="0">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="LEVELDATA" minOccurs="0" type="xsd:normalizedString"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
I am getting the below error while validating this XSD with XML,
cvc-complex-type.2.4.a: Invalid content was found starting with element 'standardHeader'. One of '{standardHeader}' is expected.
How can I provide this standardHeader tag:
targetNamespace="http://tow.gow.ho.com/2009/08/StandardHeader/"
The problem is the wrong 'namespace' in the XML... (line 2)
When you remove it (or correct it) - it's correct.
Online-Validator
https://www.freeformatter.com/xml-validator-xsd.html
Sample XML-Generator (from XSD)
http://xsd2xml.com/
Namespace-Doc
https://www.w3schools.com/XML/xml_namespaces.asp
One possible solution for this problem is splitting the XSD into two files:
One for the first absent namespace (a.xsd) and one for the second namespace (b.xsd) which is declared by targetNamespace=... in b.xsd while a.xsd does not need that declaration. The second file with its own namespace is imported with the <xsd:import namespace="..." instruction. This namespace has also to be included in a namespace declaration of the xsd:schema element (here xmlns:std).
So a.xsd is:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:std="http://tow.gow.ho.com/2009/08/StandardHeader">
<xsd:import namespace="http://tow.gow.ho.com/2009/08/StandardHeader"
schemaLocation="b.xsd" />
<xsd:element name="myTest">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element ref="std:standardHeader" minOccurs="0">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
And b.xsd is:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://tow.gow.ho.com/2009/08/StandardHeader">
<xsd:element name="standardHeader">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="data" minOccurs="0">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="LEVELDATA" minOccurs="0" type="xsd:normalizedString"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
These two XSDs do validate your XML file with its namespace as it is.
This answer was inspired by this webpage: "Multi-Schema Project:
Zero, One, or Many Namespaces?". It does not copy the solutions 1-to-1, but is rather inspired by that webpage.

'OR' operation on element type in XSD

This is a continuity of My question
Assume below is my xsd file,
Updated XSD file
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/wm" xmlns="http://www.example.com/wm" elementFormDefault="qualified" version="1.0">
<xsd:include schemaLocation="Comm.xsd" />
<xsd:element name="testEame1">
<xsd:annotation>
<xsd:documentation> test </xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="id" type="xsd:string" minOccurs="1" />
<xsd:element name="session" type="rawSess" minOccurs="1" />
</xsd:sequence>
<xsd:attribute name="pid" type="xsd:integer" use="required" />
<xsd:attribute name="version" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
references another XSD which has type defined,
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://www.example.com/wm" xmlns="http://www.example.com/wm" elementFormDefault="qualified">
<xsd:complexType name="rawSess">
<xsd:sequence>
<xsd:element name="oldSessionVersion" type="Sessiontype1" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="sessInfo">
<xsd:sequence>
<xsd:element name="newSessionVersion" type="Sessiontype2" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Sessiontype1">
<xsd:sequence>
<xsd:element name="ele1" type="xsd:string" minOccurs="0" />
<xsd:element name="ele2" type="xsd:string" />
<xsd:element name="ele3" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Sessiontype2">
<xsd:sequence>
<xsd:element name="nele1" type="xsd:integer" minOccurs="0" />
<xsd:element name="nele2" type="xsd:integer" />
<xsd:element name="nele3" type="xsd:integer" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
In the above xml, for element session , I want it to have only two types like below. Either sessInfo or rawSess
<xsd:element name="session" type='sessInfo| rawSess' minOccurs="1"/>
Update Note : userDefinedtypes are complex types.
How can I configure my xsd to achieve this
If userDefinedType1 and userDefinedType2 are simple atomic types, then define a union type:
<xs:element name="session" type="one-or-two"/>
<xs:simpleType name="one-or-two">
<xs:union memberTypes="userDefinedType1 userDefinedType2"/>
</xs:simpleType>
We now know that they are actually complex types. Define a type
<xs:complexType name="eitherType">
<xs:choice>
<xs:sequence>... content model of alternative 1</xs:sequence>
<xs:sequence>... content model of alternative 2</xs:sequence>
</xs:choice>
</xs:complexType>
and reference this type from the element declaration.
NB: this only works if there is no ambiguity, that is, if the validator can work out which branch of the choice to take based on the first element name that it sees in the instance document. If your choice were between two content models that both start with an h1 element, for example, you would have to reorganise the content models to remove the ambiguity.

Restrict value of an XML attribute to different attribute's/element's value

I will start with an example
<template>
<components>
<component name="switch" />
<component name="server" />
</components>
<layout>
<grid>
<position componentName="switch" positionX="0" positionY="0" />
</grid>
</layout>
</template>
What I want is to restrict values in componentName attribute to match one of the names specified above in components. Is this possible in JAXB? Because I need to have annotated classes which are then used to generate XSD.
Given your scenario, XSD 1.0 can enforce your "referential integrity" through a key/keyref combo. However, I am not aware of what annotations there are for these constructs in JAXB (it sounds as if you're looking at generating an XSD from you Java classes); at least I never ran into such annotations (see the list here)
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="template">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="components">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="component">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="layout">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="grid">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="position">
<xsd:complexType>
<xsd:attribute name="componentName" type="xsd:string" use="required" />
<xsd:attribute name="positionX" type="xsd:unsignedByte" use="required" />
<xsd:attribute name="positionY" type="xsd:unsignedByte" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="ComponentsKey">
<xsd:selector xpath="components/component"/>
<xsd:field xpath="#name"/>
</xsd:key>
<xsd:keyref name="MatchComponent" refer="ComponentsKey">
<xsd:selector xpath="layout/grid/position"/>
<xsd:field xpath="#componentName"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>

Categories