【问题标题】:Xquery dont "filter" the first TAG with attributeXquery 不要“过滤”第一个带有属性的 TAG
【发布时间】:2019-01-16 16:15:23
【问题描述】:

我是 xquery 的菜鸟,我正在“测试”代码

对我来说不可能过滤一个具体的 xml。 我不确定,但第一个 TAG 似乎有问题,它包含一个属性,并且可能我解析错误......

XML 示例

<findToFileResponse xmlns="xmlapi_1.0">
        <equipment.PhysicalPort>
                <mtuValue>9728</mtuValue>
                <userLabel>RIOXXXX</userLabel>
        <displayedName>Port 1/1</displayedName>
        <siteId>10.10.10.1</siteId>
        <siteName>LO0015P1</siteName>
        <children-Set>
            <ethernetequipment.EthernetPortSpecifics>
                <actualDuplex>fd1000</actualDuplex>
                <children-Set></children-Set>
            </ethernetequipment.EthernetPortSpecifics>
        </children-Set>
    </equipment.PhysicalPort>
    <equipment.PhysicalPort>
        <mtuValue>9728</mtuValue>
        <userLabel>RIOXXXX</userLabel>
        <displayedName>Port 1/2</displayedName>
        <siteId>10.10.10.10</siteId>
        <siteName>LO0015P1</siteName>
        <children-Set>
            <ethernetequipment.EthernetPortSpecifics>
                <actualDuplex>fd1000</actualDuplex>
                <children-Set></children-Set>
            </ethernetequipment.EthernetPortSpecifics>
        </children-Set>
    </equipment.PhysicalPort>
</findToFileResponse>

例如,这个xquery(我在测试,目的是与其他具有相同第一个TAG的xml):

let $docu1 := doc("ListadoVanos.Mon_Jan_14_040000_CET_2019.xml")/findToFileResponse
return $docu1

我希望是这样的:

<equipment.PhysicalPort>
        <mtuValue>9728</mtuValue>
        <userLabel>RIOXXXX</userLabel>
        <displayedName>Port 1/1</displayedName>
        <siteId>10.10.10.1</siteId>
        <siteName>LO0015P1</siteName>
        <children-Set>
            <ethernetequipment.EthernetPortSpecifics>
                <actualDuplex>fd1000</actualDuplex>
                <children-Set></children-Set>
            </ethernetequipment.EthernetPortSpecifics>
        </children-Set>
</equipment.PhysicalPort>
<equipment.PhysicalPort>
...
</equipment.PhysicalPort>
<equipment.PhysicalPort>
...
</equipment.PhysicalPort>

...

但回答一个空的结果

有什么想法吗?你能帮我建立第一个过滤器吗?

提前致谢

我正在使用 BaseX。这是相关信息:

Compiling:
- pre-evaluate fn:doc(uri) to document-node() item: doc("ListadoVanos.Mon_Jan_14_040000_CET_... -> document-node {"ListadoVanos.Mon_Jan_14_...
- remove unknown element/attribute findToFileResponse
- pre-evaluate iter path to empty sequence: document-node {"ListadoVanos.Mon_Jan_14_... -> ()
- inline $docu1_0
- simplify FLWOR expression: ()
Optimized Query:
()
Query:
let $docu1 := doc("ListadoVanos.Mon_Jan_14_040000_CET_2019.xml")/findToFileResponse return $docu1
Result:
- Hit(s): 0 Items
- Updated: 0 Items
- Printed: 0 b
- Read Locking: ListadoVanos.Mon_Jan_14_040000_CET_2019.xml
- Write Locking: (none)
Timing:
- Parsing: 0.26 ms
- Compiling: 123.72 ms
- Evaluating: 0.17 ms
- Printing: 0.01 ms
- Total Time: 124.17 ms
Query plan:
<QueryPlan compiled="true" updating="false">
  <Empty size="0" type="empty-sequence()"/>
</QueryPlan>

【问题讨论】:

    标签: xml xpath xquery


    【解决方案1】:

    您遇到了一个常见问题:选择位于命名空间中的 XML。当您选择/findToFileResponse时,您选择的是一个名为findToFileResponse的元素,但由于没有指定命名空间前缀,因此假设您要选择没有命名空间的元素,这很常见。

    但是,您可以看到您的 XML 文档位于命名空间中,因为它使用了 default namespace attribute,这导致其自身及其所有后代都包含在该命名空间中(除非另有说明):

    xmlns="xmlapi_1.0"
    

    要选择此命名空间中的元素,首先需要在 XQuery 序言中输入declare it and assign it to a prefix

    declare namespace api = "xmlapi_1.0";
    

    现在您可以使用前缀选择该命名空间中的元素:

    /api:findToFileResponse
    

    您还可以使用通配符前缀选择任何命名空间中的元素:

    /*:findToFileResponse
    

    【讨论】:

      猜你喜欢
      • 2012-07-05
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 2015-02-11
      • 2021-01-28
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多