【问题标题】:XPath Expression to select an specific node用于选择特定节点的 XPath 表达式
【发布时间】:2012-01-23 22:30:56
【问题描述】:

我在尝试构建 Xpath 表达式以选择所有“字段”节点“名称”值属性时遇到问题。

我已经尝试过://field[@name] 但它没有返回任何匹配项。

我需要来自 XML 文档的示例结果是:currentDate、reallyYours、claimant、dateOfLoss 等...

感谢您的帮助。

<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="AdobeLiveCycleDesignerES_V9.0.0.2.20101008.1.734229" APIVersion="3.1.20001.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2012-01-23T17:23:59Z" uuid="594e6d9b-2319-4fdb-b490-4d7655e56aa0">
<template xmlns="http://www.xfa.org/schema/xfa-template/2.4/">
   <?formServer defaultPDFRenderFormat acrobat7.0.5static?>
   <subform name="form1" locale="en_US" layout="tb">
      <pageSet>
         <pageArea>
            <contentArea name="ContentArea1" x="0pt" y="0pt" w="612pt" h="792pt"/>
            <medium short="612pt" long="792pt" stock="custom"/>
         </pageArea>
      </pageSet>
      <subform name="DynamicFragment">
         <bind match="none"/>
         <field name="currentDate" y="0mm" x="0.86mm" w="62mm" h="0.229167in" access="readOnly">
            <ui>
               <textEdit>
                  <border hand="right" presence="hidden">
                     <?templateDesigner StyleID aped0?></border>
                  <margin/>
               </textEdit>
            </ui>
            <font typeface="Myriad Pro"/>
            <para vAlign="middle"/>
            <?PDF_OBJR TextField1[3]?>
            <bind match="global"/>
         </field>
         <field name="trulyYours" y="129.578mm" w="62mm" h="0.229167in" access="readOnly">
            <ui>
               <textEdit>
                  <border hand="right" presence="hidden">
                     <?templateDesigner StyleID aped0?></border>
                  <margin/>
               </textEdit>
            </ui>
            <font typeface="Myriad Pro"/>
            <para vAlign="middle"/>
            <?PDF_FIELD_ID 30003?>
            <?PDF_OBJR TextField1[3]?>
            <bind match="global"/>
         </field>
         <field name="claimant" y="47.624mm" x="46.135mm" w="62mm" h="0.229167in" access="readOnly">
            <ui>
               <textEdit>
                  <border hand="right" presence="hidden">
                     <?templateDesigner StyleID aped0?></border>
                  <margin/>
               </textEdit>
            </ui>
            <font typeface="Myriad Pro"/>
            <para vAlign="middle"/>
            <?PDF_FIELD_ID 30003?>
            <?PDF_OBJR TextField1[3]?>
            <bind match="global"/>
         </field>
         <field name="ourInsured" y="42.618mm" x="46.135mm" w="62mm" h="0.229167in" access="readOnly">
            <ui>
               <textEdit>
                  <border hand="right" presence="hidden">
                     <?templateDesigner StyleID aped0?></border>
                  <margin/>
               </textEdit>
            </ui>
            <font typeface="Myriad Pro"/>
            <para vAlign="middle"/>
            <?PDF_FIELD_ID 30002?>
            <?PDF_OBJR TextField1[2]?>
            <bind match="global"/>
         </field>
         <field name="dateOfLoss" y="37.127mm" x="46.135mm" w="62mm" h="0.229167in" access="readOnly">
            <ui>
               <textEdit>
                  <border hand="right" presence="hidden">
                     <?templateDesigner StyleID aped0?></border>
                  <margin/>
               </textEdit>
            </ui>
            <font typeface="Myriad Pro"/>
            <para vAlign="middle"/>
            <?PDF_FIELD_ID 30001?>
            <?PDF_OBJR TextField1[1]?>
            <bind match="global"/>
         </field>
         <field name="ourClaimNumber" y="32.132mm" x="46.153mm" w="62mm" h="0.229167in" access="readOnly">
            <ui>
               <textEdit>
                  <border hand="right" presence="hidden">
                     <?templateDesigner StyleID aped0?></border>
                  <margin/>
               </textEdit>
            </ui>
            <font typeface="Myriad Pro"/>
            <para vAlign="middle"/>
            <?PDF_FIELD_ID 30001?>
            <?PDF_OBJR TextField1[0]?>
            <bind match="global"/>
         </field>
         <traversal>
            <traverse operation="first" ref="currentDate[0]"/>
         </traversal>
         <?templateDesigner isFragment yes?>
         <?templateDesigner fragmentTitle 3016DynamicFragment?></subform>
   </subform>
   <?templateDesigner FormTargetVersion 24?>
   <?templateDesigner Zoom 89?>
   <?templateDesigner Rulers horizontal:1, vertical:1, guidelines:1, crosshairs:0?></template>
<config xmlns="http://www.xfa.org/schema/xci/1.0/">
   <agent name="designer">
      <!--  [0..n]  -->
      <destination>pdf</destination>
      <pdf>
         <!--  [0..n]  -->
         <fontInfo/>
      </pdf>
   </agent>
   <present>
      <!--  [0..n]  -->
      <destination>pdf</destination>
      <pdf>
         <!--  [0..n]  -->
         <fontInfo/>
         <version>1.65</version>
      </pdf>
      <xdp>
         <packets>*</packets>
      </xdp>
   </present>
</config>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.437709, 2010/08/04-17:30:07        ">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/" rdf:about="">
         <xmp:MetadataDate>2012-01-23T17:23:59Z</xmp:MetadataDate>
         <xmp:CreatorTool>Adobe LiveCycle Designer ES 9.0</xmp:CreatorTool>
      </rdf:Description>
      <rdf:Description xmlns:pdf="http://ns.adobe.com/pdf/1.3/" rdf:about="">
         <pdf:Producer>Adobe LiveCycle Designer ES 9.0</pdf:Producer>
      </rdf:Description>
      <rdf:Description xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" rdf:about="">
         <xmpMM:DocumentID>uuid:594e6d9b-2319-4fdb-b490-4d7655e56aa0</xmpMM:DocumentID>
      </rdf:Description>
      <rdf:Description xmlns:lcd="http://ns.adobe.com/livecycle/designer/" xmlns:dc="http://purl.org/dc/elements/1.1/" rdf:about="">
         <lcd:fragmentCatalog>
            <rdf:Seq>
               <rdf:li rdf:parseType="Resource">
                  <dc:title>
                     <rdf:Alt>
                        <rdf:li xml:lang="x-default">3016DynamicFragment</rdf:li>
                     </rdf:Alt>
                  </dc:title>
                  <dc:description>
                     <rdf:Alt>
                        <rdf:li xml:lang="x-default"/>
                     </rdf:Alt>
                  </dc:description>
                  <dc:identifier>$template.form1.DynamicFragment</dc:identifier>
                  <lcd:fragmentRole>Subform</lcd:fragmentRole>
               </rdf:li>
            </rdf:Seq>
         </lcd:fragmentCatalog>
      </rdf:Description>
   </rdf:RDF>
</x:xmpmeta>
<localeSet xmlns="http://www.xfa.org/schema/xfa-locale-set/2.1/">
   <locale name="en_US" desc="English (United States)">
      <calendarSymbols name="gregorian">
         <monthNames>
            <month>January</month>
            <month>February</month>
            <month>March</month>
            <month>April</month>
            <month>May</month>
            <month>June</month>
            <month>July</month>
            <month>August</month>
            <month>September</month>
            <month>October</month>
            <month>November</month>
            <month>December</month>
         </monthNames>
         <monthNames abbr="1">
            <month>Jan</month>
            <month>Feb</month>
            <month>Mar</month>
            <month>Apr</month>
            <month>May</month>
            <month>Jun</month>
            <month>Jul</month>
            <month>Aug</month>
            <month>Sep</month>
            <month>Oct</month>
            <month>Nov</month>
            <month>Dec</month>
         </monthNames>
         <dayNames>
            <day>Sunday</day>
            <day>Monday</day>
            <day>Tuesday</day>
            <day>Wednesday</day>
            <day>Thursday</day>
            <day>Friday</day>
            <day>Saturday</day>
         </dayNames>
         <dayNames abbr="1">
            <day>Sun</day>
            <day>Mon</day>
            <day>Tue</day>
            <day>Wed</day>
            <day>Thu</day>
            <day>Fri</day>
            <day>Sat</day>
         </dayNames>
         <meridiemNames>
            <meridiem>AM</meridiem>
            <meridiem>PM</meridiem>
         </meridiemNames>
         <eraNames>
            <era>BC</era>
            <era>AD</era>
         </eraNames>
      </calendarSymbols>
      <datePatterns>
         <datePattern name="full">EEEE, MMMM D, YYYY</datePattern>
         <datePattern name="long">MMMM D, YYYY</datePattern>
         <datePattern name="med">MMM D, YYYY</datePattern>
         <datePattern name="short">M/D/YY</datePattern>
      </datePatterns>
      <timePatterns>
         <timePattern name="full">h:MM:SS A Z</timePattern>
         <timePattern name="long">h:MM:SS A Z</timePattern>
         <timePattern name="med">h:MM:SS A</timePattern>
         <timePattern name="short">h:MM A</timePattern>
      </timePatterns>
      <dateTimeSymbols>GyMdkHmsSEDFwWahKzZ</dateTimeSymbols>
      <numberPatterns>
         <numberPattern name="numeric">z,zz9.zzz</numberPattern>
         <numberPattern name="currency">$z,zz9.99|($z,zz9.99)</numberPattern>
         <numberPattern name="percent">z,zz9%</numberPattern>
      </numberPatterns>
      <numberSymbols>
         <numberSymbol name="decimal">.</numberSymbol>
         <numberSymbol name="grouping">,</numberSymbol>
         <numberSymbol name="percent">%</numberSymbol>
         <numberSymbol name="minus">-</numberSymbol>
         <numberSymbol name="zero">0</numberSymbol>
      </numberSymbols>
      <currencySymbols>
         <currencySymbol name="symbol">$</currencySymbol>
         <currencySymbol name="isoname">USD</currencySymbol>
         <currencySymbol name="decimal">.</currencySymbol>
      </currencySymbols>
      <typefaces>
         <typeface name="Myriad Pro"/>
         <typeface name="Minion Pro"/>
         <typeface name="Courier Std"/>
         <typeface name="Adobe Pi Std"/>
         <typeface name="Adobe Hebrew"/>
         <typeface name="Adobe Arabic"/>
         <typeface name="Adobe Thai"/>
         <typeface name="Kozuka Gothic Pro-VI M"/>
         <typeface name="Kozuka Mincho Pro-VI R"/>
         <typeface name="Adobe Ming Std L"/>
         <typeface name="Adobe Song Std L"/>
         <typeface name="Adobe Myungjo Std M"/>
      </typefaces>
   </locale>
</localeSet></xdp:xdp>

【问题讨论】:

    标签: xpath


    【解决方案1】:

    您显示的表达式 -- //field[@name] -- 选择所有具有 name 属性的 field 元素。您需要做的是选择属性本身。像这样:

    //field/@name
    

    关于命名空间:您的目标节点位于 http://www.xfa.org/schema/xfa-template/2.4/ 命名空间中。任何 XPath 表达式都必须通过以下步骤考虑到这一点:

    1. 将命名空间注册为 XPath 引擎的前缀。如何做到这一点取决于您用于评估表达式的宿主语言。例如,在 Java 中,这是通过创建一个注册到 javax.xml.xpath.XPath 实例的 javax.xml.namespace.NamespaceContext 来完成的。

    2. 使用您在步骤 1) 中注册的前缀来查询目标命名空间中的元素。因此,例如,如果您要使用前缀 xfa 注册 http://www.xfa.org/schema/xfa-template/2.4/,那么您将编写如下表达式:

      //xfa:field/@name
      

    与命名空间无关的解决方案:可能(但不一定推荐)选择目标节点而不考虑使用以下表达式的命名空间:

    //*[local-name()='field']/@name
    

    【讨论】:

    • @user1165912 - 这是因为您的节点位于默认命名空间中。查看我的编辑。
    【解决方案2】:

    您需要为命名空间分配前缀。 field 元素不在空命名空间中。它似乎在http://www.xfa.org/schema/xfa-template/2.4/

    如何分配命名空间前缀取决于您用于发出 XPath 的主机语言。你不说,我们帮不了你。它可能看起来像这个虚构的例子:

    xpather.registerNamespace('xfa', 'http://www.xfa.org/schema/xfa-template/2.4/');
    

    请注意,“xfa”可以是您想要的任何前缀。只有命名空间 uri 很重要。

    完成后,您的 XPath 将如下所示:

    //xfa:field/@name
    

    如果您也为 xpd 命名空间分配前缀,则可以更具体:

    /xpd:xpd/xfa:template//xfa:field/@name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-03
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2017-01-23
      相关资源
      最近更新 更多