
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
io.github.xs-parser:xs-parser
Advanced tools
xs-parser is a Java software library that represents the object model described in the [W3C XML Schema Definition Language (XSD) 1.1 Part 1: Structures](https://www.w3.org/TR/xmlschema11-1/) and [Part 2: Datatypes](https://www.w3.org/TR/xmlschema11-2/).
xs-parser
is a Java software library that represents the object model described in the W3C XML Schema Definition Language (XSD) 1.1 Part 1: Structures and Part 2: Datatypes.
xs-parser
aims to reduce the mysticism surrounding the complexity of the XSD 1.1 specification by providing users with direct access to the XSD properties.
Due to the complex structure of XML schema documents and the potential variety of the structure of schemas, there is no preference for any particular schema component or structure. Therefore, xs-parser
only provides the properties described in the XSD 1.1 specification.
Unlike other software libraries that attempt to model XML schema documents, xs-parser
does not sugarcoat the complexity inherent to the XSD 1.1 specification, but in returns offers a high level of fidelity to the specification. Another added benefit of this approach is that developers who are familiar with the XSD 1.1 specification may quickly adopt this library into their development tool chains.
xs-parser
requires Java 8 or later and has no third-party dependencies.
If Saxon, i.e. net.sf.saxon:Saxon-HE
, version 9+ is detected on the classpath at runtime, then it is used as the XPath, XQuery, and XSLT engine.
When not detected, XSLT and XQuery evaluation are disabled and the XPath engine defaults to the JAXP XPath 1.0 implementation provided by the Java Platform.
Chameleon inclusion, xs:override
, and xs:redefine
are implemented with XSLT templates, and are therefore non-functional when Saxon is not present on the classpath.
build.gradle
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'io.github.xs-parser:xs-parser:3.0'
}
Runner.java
import java.io.File;
import xs.parser.*;
public class Runner {
public static void main(final String[] args) throws IOException {
final Schema schema = new Schema(new File("/path/to/schema.xsd"));
schema.typeDefinitions().forEach(t ->
System.out.println((t instanceof ComplexType ? "Complex" : "Simple")
+ "Type definition: " + t.name()));
schema.attributeDeclarations().forEach(a ->
System.out.println("Attribute declaration: " + a.name()));
}
}
The classes of xs-parser
do not override equals()
or hashCode()
. Use reference equality, ==
, instead. Consider the following code and schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="A" />
<xs:complexType name="B" >
<xs:sequence>
<xs:element name="E" />
</xs:sequence>
</xs:complexType>
<xs:element name="E1" type="A" />
<xs:element name="E2" type="B" />
</xs:schema>
where /path/to/schema.xsd
contains the above schema.
final Schema schema = new Schema(new File("/path/to/schema.xsd"));
final TypeDefinition a = schema.typeDefinitions().getFirst();
System.out.println(a.baseTypeDefinition() == ComplexType.xsAnyType()); // Prints: true
final Element e1 = schema.elementDeclarations().getFirst();
System.out.println(e1.name()); // Prints: E1
System.out.println(e1.typeDefinition() == a); // Prints: true
final Element e2 = schema.elementDeclarations().getLast();
System.out.println(e2.name()); // Prints: E2
final TypeDefinition b = schema.typeDefinitions().getLast();
System.out.println(e2.typeDefinition() == b); // Prints: true
Almost all evaluations in xs-parser
are lazy, accomplished through a deferred execution model. The Schema
constructor does not do anything more than load the DOM of the file as well as a few sanity checks to ensure that what was provided looks roughly like an actual schema. The definitions and declarations of the schema are not parsed until they are needed.
XSD Primitive Datatypes | Java class |
---|---|
xs:string , xs:duration , xs:dateTime , xs:time , xs:date , xs:gYearMonth , xs:gYear , xs:gMonthDay , xs:gDay , xs:gMonth , xs:hexBinary , xs:base64Binary , xs:anyURI | java.lang.String |
xs:boolean | java.lang.Boolean |
xs:decimal , xs:float , xs:double | java.math.BigDecimal |
xs:QName , xs:NOTATION | javax.xml.namespace.QName |
XSD Other Built-in Datatypes | Java class |
---|---|
xs:normalizedString , xs:token , xs:language , xs:NMTOKEN , xs:NMTOKENS , xs:Name , xs:NCName , xs:ID , xs:IDREF , xs:IDREFS , xs:ENTITY , xs:ENTITIES , xs:yearMonthDuration , xs:dayTimeDuration , xs:dateTimeStamp | java.lang.String |
xs:integer , xs:nonPositiveInteger , xs:negativeInteger , xs:long , xs:int , xs:short , xs:byte , xs:nonNegativeInteger , xs:unsignedLong , xs:unsignedInt , xs:unsignedShort , xs:unsignedByte , xs:positiveInteger | java.lang.BigInteger |
final var schema = new xs.parser.Schema(new java.io.File("/path/to/schema.xsd"));
final var root = xs.parser.x.NodeSet.of(schema);
// Performs the XPath evaluation for the root schema and all imported or included schemas
final var allSchemas = root.xpath("fn:collection()/xs:schema");
// Gets all xs:complexType name attributes
// Note: XPath and XQuery usage can be mixed
// However, Saxon-HE must be on the classpath to use XQuery
final var complexTypeNames = allSchemas.xquery("xs:complexType").xpath("@name");
// Only the /path/to/schema.xsd schema file is evaluated
// Will not execute the given XPath for any imported or included schemas
final var rootSchema = root.xpath("/xs:schema");
// Gets all xs:simpleTypes in the /path/to/schema.xsd file
final var simpleTypes = rootSchema.xpath("xs:simpleType");
System.out.println("xs:complexType size: " + complexTypeNames.size());
System.out.println("xs:simpleType size: " + simpleTypes.size());
// The split() method creates a new single-element NodeSet for every element
complexTypeNames.split().forEach(name ->
System.out.println("Name: " + name.getStringValue()));
// The stream() method iterates over the underlying org.w3c.dom.Node elements
simpleTypes.stream().forEach(node ->
System.out.println("Name: " + node.getAttributes().getNamedItemNS(null, "name")));
FAQs
xs-parser is a Java software library that represents the object model described in the [W3C XML Schema Definition Language (XSD) 1.1 Part 1: Structures](https://www.w3.org/TR/xmlschema11-1/) and [Part 2: Datatypes](https://www.w3.org/TR/xmlschema11-2/).
We found that io.github.xs-parser:xs-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.