【问题标题】:Groovy gpath not working when used with prefixed xml tags与前缀 xml 标记一起使用时,Groovy gpath 不起作用
【发布时间】:2016-07-18 16:51:04
【问题描述】:

我在使用 gpath 时遇到了一些命名空间问题。

String record = '''
    <u:record>
        <u:controlfield tag="001">017003167</u:controlfield>
        <u:datafield ind2="" ind1="" tag="Tag_1234">
            <u:subfield code="a">Peter</u:subfield>
            <u:subfield code="b">10</u:subfield>
        </u:datafield>  
        <u:datafield ind2="" ind1="" tag="Tag_1234">
            <u:subfield code="a">Simon</u:subfield>
            <u:subfield code="b">20</u:subfield>
        </u:datafield>  
        <u:datafield ind2="" ind1="" tag="Tag_20">
            <u:subfield code="a">Simon</u:subfield>
            <u:subfield code="b">20</u:subfield>
        </u:datafield>          
    </u:record>
    '''

GPathResult studentRecord = new XmlSlurper(false, false).parseText(record)
GPathResult filteredRecord = studentRecord.datafield.findAll { it.'@tag' == 'Tag_1234' } 

println "filteredRecord: ${filteredRecord}\n"
println "filteredRecord size is ${filteredRecord.size()}\n"

输出为 0。不知道为什么。如果我删除 xml 前缀“u:”,gpath 工作正常。谁能告诉我哪里出错了。

【问题讨论】:

  • 什么是'recordWithPrefixXml'?
  • 这是一个错字 - 我现在已经更正了 xml。

标签: groovy gpath


【解决方案1】:

试试这个:

def studentRecord = new XmlSlurper(false, false).parseText(record)
def filteredRecord = studentRecord.'u:datafield'.findAll { it.'@tag' == 'Tag_1234' } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    相关资源
    最近更新 更多