|
Groovy example source code file (NamespaceNodeTest.groovy)
The Groovy NamespaceNodeTest.groovy source codepackage groovy.xml
/**
* Test the building of namespaced XML using GroovyMarkup
*/
class NamespaceNodeTest extends TestXmlSupport {
void testNodeBuilderWithNamespace() {
def n = new Namespace('http://foo/bar')
def builder = NamespaceBuilder.newInstance(new NodeBuilder(), n.uri)
def result = builder.outer(id: "3") {
inner(name: "foo")
inner("bar")
}
assert result[n.inner][0].@name == 'foo'
assert result[n.inner][1].text() == 'bar'
}
void testTree() {
def builder = NodeBuilder.newInstance()
def xmlns = new NamespaceBuilder(builder)
def xsd = xmlns.namespace('http://www.w3.org/2001/XMLSchema', 'xsd')
def root = xsd.schema {
annotation {
documentation("Purchase order schema for Example.com.")
}
element(name: 'purchaseOrder', type: 'PurchaseOrderType')
element(name: 'comment', type: 'xsd:string')
complexType(name: 'PurchaseOrderType') {
sequence {
element(name: 'shipTo', type: 'USAddress')
element(name: 'billTo', type: 'USAddress')
element(minOccurs: '0', ref: 'comment')
element(name: 'items', type: 'Items')
}
attribute(name: 'orderDate', type: 'xsd:date')
}
}
assert root != null
assertGPaths(root)
}
void assertGPaths(Node root) {
// check root node
def name = root.name()
assert name instanceof QName
assert name.namespaceURI == 'http://www.w3.org/2001/XMLSchema'
assert name.localPart == 'schema'
assert name.prefix == 'xsd'
// check 'xsd' qname factory
Namespace xsd = new Namespace('http://www.w3.org/2001/XMLSchema', 'xsd')
def qname = xsd.annotation
assert qname.namespaceURI == 'http://www.w3.org/2001/XMLSchema'
assert qname.localPart == 'annotation'
assert qname.prefix == 'xsd'
def docNode = root[xsd.annotation][xsd.documentation]
assert docNode[0].text() == "Purchase order schema for Example.com."
def attrNode = root[xsd.complexType][xsd.attribute]
assert attrNode[0].@name == 'orderDate'
}
}
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy NamespaceNodeTest.groovy source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.