namespacenodegpathtest.groovy

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 56 行

GROOVY
56
字号
package groovy.xml

/**
 * Test the use of GPath navigation with namespaces
 */
class NamespaceNodeGPathTest extends TestXmlSupport {
    
    void testTree() {
		Node root = new XmlParser().parseText("""
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:annotation xsd:cheese="Edam">
     <xsd:documentation>Purchase order schema for Example.com.</xsd:documentation>
  </xsd:annotation>
</xsd:schema>
""")

    		Namespace xsd = new Namespace('http://www.w3.org/2001/XMLSchema', 'xsd')

	    def children = root.children()
	    println "has children $children"
	        		
    		def name = root.name()
    		println "name is of type ${name.getClass()} with value $name"

		root.children().each { println "has a child with name ${it.name()} and content $it" }
		    		
    		def foo = xsd.annotation
    		println "qname is $foo"
    		println "qname url is $foo.namespaceURI"
    		println "qname prefix is $foo.prefix"
    		println "qname localPart is $foo.localPart"
    		
    		def a = root[xsd.annotation]
    		println "Found results $a"
    		
	    assert a.size() == 1 : " size is $a.size()"

   		def aNode = a[0]
   		def cheese = aNode.attributes()[xsd.cheese]
		assert cheese == "Edam"   		
   		println "Found namespaced attribute $cheese"

   		cheese = aNode.attribute(xsd.cheese)
		assert cheese == "Edam"   		
   		println "Found namespaced attribute $cheese"
   		    		
    		def e = root[xsd.annotation][xsd.documentation]
    		//def e = a[xsd.documentation]

	    assert e.size() == 1: " size is $e.size()"
		
    		String text = e.text()
    		println "Found element: $e with text: $text"
    		assert text == "Purchase order schema for Example.com."
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?