【问题标题】:XPath match XML node based on particular attribute valuesXPath 基于特定属性值匹配 XML 节点
【发布时间】:2021-05-10 17:00:12
【问题描述】:

我的 XPath 表达式仅匹配具有任何属性的 Property_Maps 类型的第一个节点。

//Ingest_LDD/Property_Maps/*

我想匹配一个特定的Property_Maps基于特定的属性值,所以我创建了这个Xpath:

//Ingest_LDD/Property_Maps[@identifier='identifierABCD' and @title='titleABCD' and @namespace_id='namespace_idABCD' and @description='descriptionABCD' and @external_property_map_id='external_property_map_idABCD' and @external_property_map_id='external_property_map_idABCD' and @Property_Map='identifier' and @Property_Map='identifier']

再试一次@符号:

//Ingest_LDD/Property_Maps[identifier='identifierABCD' and title='titleABCD' and namespace_id='namespace_idABCD' and description='descriptionABCD' and external_property_map_id='external_property_map_idABCD' and external_property_map_id='external_property_map_idABCD' and Property_Map='identifier' and Property_Map='identifier']

解决方案语法:

//Ingest_LDD/DD_Attribute[name='nameABCD' and version_id='version_idXYZ' and local_identifier='local_identifier2' and nillable_flag='false' and submitter_name='submitter_name2' and definition='definition8' and comment='comment5' ]

解决方案:问题在于 Java 中的 XPath 查询构建器附加了树的额外级别和“属性作为值”而不是属性=值。

进一步的问题:我们如何根据某些属性进行匹配,并取回一个元素及其所有子元素以执行向下树的递归复制?

但是,此表达式不匹配或返回结果。

示例 xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--(name , ldd_version_id , full_name , steward_id , namespace_id , external_property_maps_id* , comment? , last_modification_date_time , DD_Attribute* , DD_Class* , DD_Rule* , Property_Maps*)-->
<Ingest_LDD>
    <name>name1</name>
    <ldd_version_id>ldd_version_id1</ldd_version_id>
    <full_name>full_name1</full_name>
    <steward_id>steward_id1</steward_id>
    <namespace_id>namespace_id1</namespace_id>
    <external_property_maps_id>external_property_maps_id1</external_property_maps_id>
    <external_property_maps_id>external_property_maps_id2</external_property_maps_id>
    <comment>comment1</comment>
    <last_modification_date_time>last_modification_date_time1</last_modification_date_time>

   <!--(name , version_id , local_identifier , submitter_name , definition , abstract_flag? , element_flag? , Internal_Reference* , (DD_Association | DD_Association_External)+ , Terminological_Entry*)-->
   <DD_Class>
        <name>name40</name>
        <version_id>version_id3</version_id>
        <local_identifier>local_identifier3</local_identifier>
        <submitter_name>submitter_name3</submitter_name>
        <definition>definition15</definition>
        <abstract_flag>false</abstract_flag>
        <element_flag>false</element_flag>
      <!--((lid_reference | lidvid_reference) , reference_type , comment?)-->
      <Internal_Reference>
            <lidvid_reference>lidvid_reference_4</lidvid_reference>
            <reference_type>reference_type5</reference_type>
            <comment>comment8</comment>
        </Internal_Reference>
      <!--((lid_reference | lidvid_reference) , reference_type , comment?)-->
      <Internal_Reference>
            <lidvid_reference>lidvid_reference_5</lidvid_reference>
            <reference_type>reference_type6</reference_type>
            <comment>comment9</comment>
        </Internal_Reference>
        <DD_Association_External>
            <name>name41</name>
            <namespace_id>namespace_id2</namespace_id>
            <reference_type>reference_type7</reference_type>
            <minimum_occurrences>minimum_occurrences1</minimum_occurrences>
            <maximum_occurrences>maximum_occurrences1</maximum_occurrences>
        </DD_Association_External>
      <!--(identifier_reference+ , local_identifier* , reference_type , minimum_occurrences , maximum_occurrences , constant_value? , DD_Attribute_Reference? , DD_Class_Reference?)-->
      <DD_Association>
            <identifier_reference>identifier_reference1</identifier_reference>
            <identifier_reference>identifier_reference2</identifier_reference>
            <local_identifier>local_identifier4</local_identifier>
            <local_identifier>local_identifier5</local_identifier>
            <reference_type>reference_type8</reference_type>
            <minimum_occurrences>minimum_occurrences2</minimum_occurrences>
            <maximum_occurrences>maximum_occurrences2</maximum_occurrences>
            <constant_value>constant_value1</constant_value>
            <DD_Attribute_Reference>
                <namespace_id>namespace_id3</namespace_id>
                <name>name42</name>
            </DD_Attribute_Reference>
            <DD_Class_Reference>
                <namespace_id>namespace_id4</namespace_id>
                <name>name43</name>
            </DD_Class_Reference>
        </DD_Association>
      <!--(name , definition , language , preferred_flag , instance_id? , External_Reference_Extended*)-->
      <Terminological_Entry>
            <name>name44</name>
            <definition>definition16</definition>
            <language>language13</language>
            <preferred_flag>true</preferred_flag>
            <instance_id>instance_id13</instance_id>
         <!--((doi? , reference_text , description?) , (name? , url?))-->
         <External_Reference_Extended>
                <doi>doi25</doi>
                <reference_text>reference_text25</reference_text>
                <description>description25</description>
                <name>name45</name>
                <url>url25</url>
            </External_Reference_Extended>
         <!--((doi? , reference_text , description?) , (name? , url?))-->
         <External_Reference_Extended>
                <doi>doi26</doi>
                <reference_text>reference_text26</reference_text>
                <description>description26</description>
                <name>name46</name>
                <url>url26</url>
            </External_Reference_Extended>
        </Terminological_Entry>
      <!--(name , definition , language , preferred_flag , instance_id? , External_Reference_Extended*)-->
      <Terminological_Entry>
            <name>name47</name>
            <definition>definition17</definition>
            <language>language14</language>
            <preferred_flag>true</preferred_flag>
            <instance_id>instance_id14</instance_id>
         <!--((doi? , reference_text , description?) , (name? , url?))-->
         <External_Reference_Extended>
                <doi>doi27</doi>
                <reference_text>reference_text27</reference_text>
                <description>description27</description>
                <name>name48</name>
                <url>url27</url>
            </External_Reference_Extended>
         <!--((doi? , reference_text , description?) , (name? , url?))-->
         <External_Reference_Extended>
                <doi>doi28</doi>
                <reference_text>reference_text28</reference_text>
                <description>description28</description>
                <name>name49</name>
                <url>url28</url>
            </External_Reference_Extended>
        </Terminological_Entry>
    </DD_Class>
   <!--(name , version_id , local_identifier , submitter_name , definition , abstract_flag? , element_flag? , Internal_Reference* , (DD_Association | DD_Association_External)+ , Terminological_Entry*)-->
   <DD_Class>
        <name>name50</name>
        <version_id>version_id4</version_id>
        <local_identifier>local_identifier6</local_identifier>
        <submitter_name>submitter_name4</submitter_name>
        <definition>definition18</definition>
        <abstract_flag>false</abstract_flag>
        <element_flag>true</element_flag>
      <!--((lid_reference | lidvid_reference) , reference_type , comment?)-->
      <Internal_Reference>
            <lidvid_reference>lidvid_reference_6</lidvid_reference>
            <reference_type>reference_type9</reference_type>
            <comment>comment10</comment>
        </Internal_Reference>
      <!--((lid_reference | lidvid_reference) , reference_type , comment?)-->
      <Internal_Reference>
            <lidvid_reference>lidvid_reference_7</lidvid_reference>
            <reference_type>reference_type10</reference_type>
            <comment>comment11</comment>
        </Internal_Reference>
        <DD_Association_External>
            <name>name51</name>
            <namespace_id>namespace_id5</namespace_id>
            <reference_type>reference_type11</reference_type>
            <minimum_occurrences>minimum_occurrences3</minimum_occurrences>
            <maximum_occurrences>maximum_occurrences3</maximum_occurrences>
        </DD_Association_External>
      <!--(identifier_reference+ , local_identifier* , reference_type , minimum_occurrences , maximum_occurrences , constant_value? , DD_Attribute_Reference? , DD_Class_Reference?)-->
      <DD_Association>
            <identifier_reference>identifier_reference3</identifier_reference>
            <identifier_reference>identifier_reference4</identifier_reference>
            <local_identifier>local_identifier7</local_identifier>
            <local_identifier>local_identifier8</local_identifier>
            <reference_type>reference_type12</reference_type>
            <minimum_occurrences>minimum_occurrences4</minimum_occurrences>
            <maximum_occurrences>maximum_occurrences4</maximum_occurrences>
            <constant_value>constant_value2</constant_value>
            <DD_Attribute_Reference>
                <namespace_id>namespace_id6</namespace_id>
                <name>name52</name>
            </DD_Attribute_Reference>
            <DD_Class_Reference>
                <namespace_id>namespace_id7</namespace_id>
                <name>name53</name>
            </DD_Class_Reference>
        </DD_Association>
      <!--(name , definition , language , preferred_flag , instance_id? , External_Reference_Extended*)-->
      <Terminological_Entry>
            <name>name54</name>
            <definition>definition19</definition>
            <language>language15</language>
            <preferred_flag>true</preferred_flag>
            <instance_id>instance_id15</instance_id>
         <!--((doi? , reference_text , description?) , (name? , url?))-->
         <External_Reference_Extended>
                <doi>doi29</doi>
                <reference_text>reference_text29</reference_text>
                <description>description29</description>
                <name>name55</name>
                <url>url29</url>
            </External_Reference_Extended>
         <!--((doi? , reference_text , description?) , (name? , url?))-->
         <External_Reference_Extended>
                <doi>doi30</doi>
                <reference_text>reference_text30</reference_text>
                <description>description30</description>
                <name>name56</name>
                <url>url30</url>
            </External_Reference_Extended>
        </Terminological_Entry>
      <!--(name , definition , language , preferred_flag , instance_id? , External_Reference_Extended*)-->
      <Terminological_Entry>
            <name>name57</name>
            <definition>definition20</definition>
            <language>language16</language>
            <preferred_flag>true</preferred_flag>
            <instance_id>instance_id16</instance_id>
         <!--((doi? , reference_text , description?) , (name? , url?))-->
         <External_Reference_Extended>
                <doi>doi31</doi>
                <reference_text>reference_text31</reference_text>
                <description>description31</description>
                <name>name58</name>
                <url>url31</url>
            </External_Reference_Extended>
         <!--((doi? , reference_text , description?) , (name? , url?))-->
         <External_Reference_Extended>
                <doi>doi32</doi>
                <reference_text>reference_text32</reference_text>
                <description>description32</description>
                <name>name59</name>
                <url>url32</url>
            </External_Reference_Extended>
        </Terminological_Entry>
    </DD_Class>
   <!--(local_identifier , rule_context , rule_assign* , DD_Attribute_Reference? , DD_Class_Reference? , DD_Rule_Statement+)-->
   <DD_Rule>
        <local_identifier>local_identifier9</local_identifier>
        <rule_context>rule_context1</rule_context>
        <rule_assign>rule_assign1</rule_assign>
        <rule_assign>rule_assign2</rule_assign>
        <DD_Attribute_Reference>
            <namespace_id>namespace_id8</namespace_id>
            <name>name60</name>
        </DD_Attribute_Reference>
        <DD_Class_Reference>
            <namespace_id>namespace_id9</namespace_id>
            <name>name61</name>
        </DD_Class_Reference>
      <!--(rule_type , rule_test , rule_message , rule_description? , rule_value*)-->
      <DD_Rule_Statement>
            <rule_type>rule_type1</rule_type>
            <rule_test>rule_test1</rule_test>
            <rule_message>rule_message1</rule_message>
            <rule_description>rule_description1</rule_description>
            <rule_value>rule_value1</rule_value>
            <rule_value>rule_value2</rule_value>
        </DD_Rule_Statement>
      <!--(rule_type , rule_test , rule_message , rule_description? , rule_value*)-->
      <DD_Rule_Statement>
            <rule_type>rule_type2</rule_type>
            <rule_test>rule_test2</rule_test>
            <rule_message>rule_message2</rule_message>
            <rule_description>rule_description2</rule_description>
            <rule_value>rule_value3</rule_value>
            <rule_value>rule_value4</rule_value>
        </DD_Rule_Statement>
    </DD_Rule>
   <!--(local_identifier , rule_context , rule_assign* , DD_Attribute_Reference? , DD_Class_Reference? , DD_Rule_Statement+)-->
   <DD_Rule>
        <local_identifier>local_identifier10</local_identifier>
        <rule_context>rule_context2</rule_context>
        <rule_assign>rule_assign3</rule_assign>
        <rule_assign>rule_assign4</rule_assign>
        <DD_Attribute_Reference>
            <namespace_id>namespace_id10</namespace_id>
            <name>name62</name>
        </DD_Attribute_Reference>
        <DD_Class_Reference>
            <namespace_id>namespace_id11</namespace_id>
            <name>name63</name>
        </DD_Class_Reference>
      <!--(rule_type , rule_test , rule_message , rule_description? , rule_value*)-->
      <DD_Rule_Statement>
            <rule_type>rule_type3</rule_type>
            <rule_test>rule_test3</rule_test>
            <rule_message>rule_message3</rule_message>
            <rule_description>rule_description3</rule_description>
            <rule_value>rule_value5</rule_value>
            <rule_value>rule_value6</rule_value>
        </DD_Rule_Statement>
      <!--(rule_type , rule_test , rule_message , rule_description? , rule_value*)-->
      <DD_Rule_Statement>
            <rule_type>rule_type4</rule_type>
            <rule_test>rule_test4</rule_test>
            <rule_message>rule_message4</rule_message>
            <rule_description>rule_description4</rule_description>
            <rule_value>rule_value7</rule_value>
            <rule_value>rule_value8</rule_value>
        </DD_Rule_Statement>
    </DD_Rule>
   <!--(identifier , title? , namespace_id , description? , external_property_map_id* , Property_Map+)-->
   <Property_Maps>
        <identifier>identifier1</identifier>
        <title>title1</title>
        <namespace_id>namespace_id12</namespace_id>
        <description>description33</description>
        <external_property_map_id>external_property_map_id1</external_property_map_id>
        <external_property_map_id>external_property_map_id2</external_property_map_id>
      <!--(identifier? , title? , model_object_id , model_object_type , instance_id* , external_namespace_id* , description? , Property_Map_Entry+)-->
      <Property_Map>
            <identifier>identifier2</identifier>
            <title>title2</title>
            <model_object_id>model_object_id1</model_object_id>
            <model_object_type>model_object_type1</model_object_type>
            <instance_id>instance_id17</instance_id>
            <instance_id>instance_id18</instance_id>
            <external_namespace_id>external_namespace_id1</external_namespace_id>
            <external_namespace_id>external_namespace_id2</external_namespace_id>
            <description>description34</description>
         <!--(property_map_type? , property_map_subtype? , property_name , property_value+)-->
         <Property_Map_Entry>
                <property_map_type>property_map_type1</property_map_type>
                <property_map_subtype>property_map_subtype1</property_map_subtype>
                <property_name>property_name1</property_name>
                <property_value>property_value1</property_value>
                <property_value>property_value2</property_value>
            </Property_Map_Entry>
         <!--(property_map_type? , property_map_subtype? , property_name , property_value+)-->
         <Property_Map_Entry>
                <property_map_type>property_map_type2</property_map_type>
                <property_map_subtype>property_map_subtype2</property_map_subtype>
                <property_name>property_name2</property_name>
                <property_value>property_value3</property_value>
                <property_value>property_value4</property_value>
            </Property_Map_Entry>
        </Property_Map>
      <!--(identifier? , title? , model_object_id , model_object_type , instance_id* , external_namespace_id* , description? , Property_Map_Entry+)-->
      <Property_Map>
            <identifier>identifier3</identifier>
            <title>title3</title>
            <model_object_id>model_object_id2</model_object_id>
            <model_object_type>model_object_type2</model_object_type>
            <instance_id>instance_id19</instance_id>
            <instance_id>instance_id20</instance_id>
            <external_namespace_id>external_namespace_id3</external_namespace_id>
            <external_namespace_id>external_namespace_id4</external_namespace_id>
            <description>description35</description>
         <!--(property_map_type? , property_map_subtype? , property_name , property_value+)-->
         <Property_Map_Entry>
                <property_map_type>property_map_type3</property_map_type>
                <property_map_subtype>property_map_subtype3</property_map_subtype>
                <property_name>property_name3</property_name>
                <property_value>property_value5</property_value>
                <property_value>property_value6</property_value>
            </Property_Map_Entry>
         <!--(property_map_type? , property_map_subtype? , property_name , property_value+)-->
         <Property_Map_Entry>
                <property_map_type>property_map_type4</property_map_type>
                <property_map_subtype>property_map_subtype4</property_map_subtype>
                <property_name>property_name4</property_name>
                <property_value>property_value7</property_value>
                <property_value>property_value8</property_value>
            </Property_Map_Entry>
        </Property_Map>
    </Property_Maps>
   <!--(identifier , title? , namespace_id , description? , external_property_map_id* , Property_Map+)-->
   <Property_Maps>
        <identifier>identifierABCD</identifier>
        <title>titleABCD</title>
        <namespace_id>namespace_idABCD</namespace_id>
        <description>descriptionABCD</description>
        <external_property_map_id>external_property_map_idABCD</external_property_map_id>
        <external_property_map_id>external_property_map_idABCD</external_property_map_id>
      <!--(identifier? , title? , model_object_id , model_object_type , instance_id* , external_namespace_id* , description? , Property_Map_Entry+)-->
      <Property_Map>
            <identifier>identifierABCD</identifier>
            <title>title5</title>
            <model_object_id>model_object_id3</model_object_id>
            <model_object_type>model_object_type3</model_object_type>
            <instance_id>instance_id21</instance_id>
            <instance_id>instance_id22</instance_id>
            <external_namespace_id>external_namespace_id5</external_namespace_id>
            <external_namespace_id>external_namespace_id6</external_namespace_id>
            <description>description37</description>
         <!--(property_map_type? , property_map_subtype? , property_name , property_value+)-->
         <Property_Map_Entry>
                <property_map_type>property_map_type5</property_map_type>
                <property_map_subtype>property_map_subtype5</property_map_subtype>
                <property_name>property_name5</property_name>
                <property_value>property_value9</property_value>
                <property_value>property_value10</property_value>
            </Property_Map_Entry>
         <!--(property_map_type? , property_map_subtype? , property_name , property_value+)-->
         <Property_Map_Entry>
                <property_map_type>property_map_type6</property_map_type>
                <property_map_subtype>property_map_subtype6</property_map_subtype>
                <property_name>property_name6</property_name>
                <property_value>property_value11</property_value>
                <property_value>property_value12</property_value>
            </Property_Map_Entry>
        </Property_Map>
      <!--(identifier? , title? , model_object_id , model_object_type , instance_id* , external_namespace_id* , description? , Property_Map_Entry+)-->
      <Property_Map>
            <identifier>identifierABCD</identifier>
            <title>title6</title>
            <model_object_id>model_object_id4</model_object_id>
            <model_object_type>model_object_type4</model_object_type>
            <instance_id>instance_id23</instance_id>
            <instance_id>instance_id24</instance_id>
            <external_namespace_id>external_namespace_id7</external_namespace_id>
            <external_namespace_id>external_namespace_id8</external_namespace_id>
            <description>description38</description>
         <!--(property_map_type? , property_map_subtype? , property_name , property_value+)-->
         <Property_Map_Entry>
                <property_map_type>property_map_type7</property_map_type>
                <property_map_subtype>property_map_subtype7</property_map_subtype>
                <property_name>property_name7</property_name>
                <property_value>property_value13</property_value>
                <property_value>property_value14</property_value>
            </Property_Map_Entry>
         <!--(property_map_type? , property_map_subtype? , property_name , property_value+)-->
         <Property_Map_Entry>
                <property_map_type>property_map_type8</property_map_type>
                <property_map_subtype>property_map_subtype8</property_map_subtype>
                <property_name>property_name8</property_name>
                <property_value>property_value15</property_value>
                <property_value>property_value16</property_value>
            </Property_Map_Entry>
        </Property_Map>
    </Property_Maps>
</Ingest_LDD>

【问题讨论】:

  • 我了解您的大部分 xpath 表达式并且可能可以修复它,但我不明白您使用 @Property_Map='identifier' 指的是哪个元素?
  • @JackFleeting,我希望能够根据特定元素的属性文本值选择任何元素。例如,我想取回具有 = identifierABCD AND =title6 的 <property_map>。查看我更新的查询。</property_map>
  • 我没有看到更新的查询。
  • @JackFleeting,刚刚发布了新查询。
  • 查询的最后一部分 (and Property_Map='identifier' and Property_Map='identifier') 没有意义。仅针对该部分:同样,您要确切选择哪个元素?请编辑您的问题并显示预期的输出。

标签: xpath


【解决方案1】:

您犯的主要错误是使用@,它选择了xml 节点的属性,而实际上您想要评估xml 节点的子节点的内容。你可能想要更多这样的东西。

//Ingest_LDD/Property_Maps[identifier='identifierABCD' and title='titleABCD' and namespace_id='namespace_idABCD' and description='descriptionABCD' and external_property_map_id='external_property_map_idABCD']

然而,正如上面评论中所指出的,@Property_Map='identifier' and @Property_Map='identifier' 的意图是什么尚不清楚。也许这是一个错字?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    相关资源
    最近更新 更多