【问题标题】:Scala, combining filters on XML nodeScala,在 XML 节点上组合过滤器
【发布时间】:2012-08-10 14:52:53
【问题描述】:

XML 示例:

<structure-node (n)>
    <node-data (1)>
        <feature-list (n)>
            <feature (n)>
                <value (1)>123</value>
            </feature>
        </feature-list>
    </node-data>
</structure-node>

括号中为最大出现次数。我想获得一个结构节点,但仅限于包含值等于 123 的地方。

我已经试过了(玩!用 Scala):

@{ (parentNode \ "structure-node").filter{ f => (f \ "node-data" \ "feature-list" \ "feature" \ "value").text == "123"}  }

但似乎我无法过滤节点本身不存在的字段。我必须组合过滤器吗?我试过了,但失败了……

有什么想法吗?

【问题讨论】:

    标签: xml scala filter xmlnode


    【解决方案1】:

    可以使用exists函数在“structure-node”标签下的“value”标签的SeqNode上添加条件:

    (parentNode \ "structure-node").filter(s => (s \ "node-data" \ "feature-list" \ "feature" \ "value").exists ( t => t.text == "123"))
    

    或

    (parentNode \ "structure-node").filter(s => (s \\ "value").exists ( t => t.text == "123"))
    

    【讨论】:

    • 这似乎只在只有一个功能列表且其中只有一个功能时才有效。我想我必须过滤和过滤再过滤......
    • 您可以通过 \\ 选择器管理许多事件,我已经编辑了我的答案来解决这个问题
    • 我从 XSLT 知道这一点并且已经尝试过了,但没有成功。你测试了吗?我的解决方案:@{(parentNode \ "structure-node").filter( fo =&gt; fo.exists {fi =&gt; (fi \ "node-data" \\ "feature-list" \\ "feature" \ "value").text == "123" } ) } 不起作用。
    • 我理解为什么这不起作用,因为如果你有很多出现的特征或特征列表文本被连接,我会检查并回复你
    • 好的,这正是我的意思。您只有一个功能列表和一个功能。但是在 中可能有 n 个 并且在那 n 个 中。然后它似乎不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多