如何使用XQuery计算以特定字符串开头的XML节点?

如何使用XQuery计算以特定字符串开头的XML节点?,xml,xpath,xquery,Xml,Xpath,Xquery,我有以下XML: <prescriptions> <prescription id="1" amka="1"> <status>closed</status> <doctor_info> <amka>111111</amka> <doc_code> 123</doc_code> <name> chris</name&

我有以下XML:

<prescriptions>
  <prescription id="1" amka="1">
    <status>closed</status>
    <doctor_info>
      <amka>111111</amka>
      <doc_code> 123</doc_code>
      <name> chris</name>
      <surname>st</surname>
      <specialization>path</specialization>
      <hospital>401</hospital>
    </doctor_info>
    <patient_info>
      <amka>1</amka>
      <genre>man</genre>
      <name>elton </name>
      <surname>kev</surname>
      <age>28</age>
      <adress>aaa123</adress>
      <home_phone>210</home_phone>
      <cell_phone>69</cell_phone>
      <email>aaa@aaa.com</email>
      <insurance>diom</insurance>
    </patient_info>
    <main_prescription>
      <type>eee</type>
      <epanalipseis>2</epanalipseis>
      <date>21/1/2012</date>
      <comms>blablabla</comms>
      <diagnosh>kkkk</diagnosh>
      <drug1>
        <onoma>prezza</onoma>
        <drastikh_ousia>llll</drastikh_ousia>
        <posotita>12</posotita>
        <dosologia>
          <arithmos_hapion>5</arithmos_hapion>
          <ores>2</ores>
          <hmeres>21</hmeres>
        </dosologia>
        <simetoxi>25%</simetoxi>
        <tropos_xorigisis>ass</tropos_xorigisis>
        <cost>100</cost>
      </drug1>
      <drug2>
        <onoma>prezza</onoma>
        <drastikh_ousia>llll</drastikh_ousia>
        <posotita>12</posotita>
        <dosologia>
          <arithmos_hapion>5</arithmos_hapion>
          <ores>2</ores>
          <hmeres>21</hmeres>
        </dosologia>
        <simetoxi>25%</simetoxi>
        <tropos_xorigisis>ass</tropos_xorigisis>
        <cost>100</cost>
      </drug2>
      <drug3>
        <onoma>prezza</onoma>
        <drastikh_ousia>llll</drastikh_ousia>
        <posotita>12</posotita>
        <dosologia>
          <arithmos_hapion>5</arithmos_hapion>
          <ores>2</ores>
          <hmeres>21</hmeres>
        </dosologia>
        <simetoxi>25%</simetoxi>
        <tropos_xorigisis>ass</tropos_xorigisis>
        <cost>100</cost>
      </drug3>
      <total_cost>100</total_cost>
      <prospou>nosokomeio</prospou>
      <drug_comms>ablablabla</drug_comms>
    </main_prescription>
  </prescription>
</prescriptions>
试试这个

'计数(/prescriptions/prescription/main_prescription/*[匹配项(名称(),'^drug[0-9]+$))

在这里,我们选择所有节点名称为“drug”并以整数结尾的节点

您可以在下面的链接上进一步了解xpath函数。

即使使用XPath 1.0表达式,也可以选择想要的节点

/*/*/main_prescription
      /*[starts-with(name(), 'drug')
       and
          substring-after(name(), 'drug')
         =
          floor(substring-after(name(), 'drug'))
        ]
这将选择所有符合以下条件的元素:

  • 主元素
    元素的子元素,该元素是文档顶部元素的子元素,并且:

  • 名称以字符串
    “drug”
    开头,而
    “drug”
    后面的剩余字符串是一个整数

  • 说明

    /*/*/main_prescription
          /*[starts-with(name(), 'drug')
           and
              substring-after(name(), 'drug')
             =
              floor(substring-after(name(), 'drug'))
            ]
    
  • 正确使用函数
    以()开头
    name()
    substring-after()

  • $x
    的字符串值表示整数时,表达式
    floor($x)=$x
    的计算结果正好是
    true()


  • SQL Server 2008似乎不同意给出结果的语法:“0-9”附近的语法不正确。必须使用转义字符。我已经修改了文件answer@KaipaMSarma:我认为此表达式不选择元素,例如
    drug99
    。正确的正则表达式是:
    ^drug[0-9]+$
    Hi,Will-floor函数接受字符串作为参数。如果遇到字符串作为参数,会发生什么情况。@KaipaMSarma:在XPath 2.0(强类型)中,将非数字参数传递给
    floor()
    肯定会导致错误。在XPath1.0中,它的类型非常弱,
    floor('3.5')
    返回数字3<代码>楼层(“高”)返回
    NaN
    。也就是说,将参数转换为数字,然后应用函数。确定。。现在明白了。我想知道为什么它会为我抛出错误。@KaipaMSarma:在XPath 2.0中,必须使用explicit
    xs:integer()
    将子字符串转换为整数。