1
0
-1

I am trying to parse an XML document that includes namespaces using the XmlDoc class in fluent, but I can't figure out what XPath to use when referring to elements that have a namespace defined:

import com.avoka.tm.util.*
String startXml = """<ns1:root ns1='www.google.com'>
<ns1:data1>test</ns1:data1>
<data2>test</data2>
</ns1:root>"""
XmlDoc doc = new XmlDoc(startXml)
println "data1: ${doc.getText('//data1')}" //returns null
println "data2: ${doc.getText('//data2')}" //returns 'test'
println "data1 with namespace: ${doc.getText('//ns1:data1')}" //Throws XPath expression error

Is there a way to reference elements with a namespace, or is this not supported?

    CommentAdd your comment...

    2 answers

    1.  
      1
      0
      -1

      Hi Chris

       

      Try Path utility (note xmlns for defining namespace):

      import com.avoka.tm.util.*
      
      String startXml = """<ns1:root xmlns:ns1='www.google.com'>
      <ns1:data1>test1</ns1:data1>
      <data2>test2</data2>
      </ns1:root>"""
      Path path = new Path(startXml)
      println "data1: ${path.val('//data1')}" //returns null
      println "data1 with namespace: ${path.val('//*[local-name()=\"data1\"]')}" //returns test1
      println "data2: ${path.val('//data2')}" //returns test2
      println "data1 with namespace: ${path.val('//ns1:data1')}" //returns test1 

       

      Cheers
      Rado

        CommentAdd your comment...