【问题标题】:How can I count XML nodes that start with a certain string using XQuery?如何使用 XQuery 计算以某个字符串开头的 XML 节点?
【发布时间】:2012-01-25 09:37:51
【问题描述】:

我有以下 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>

&lt;main_prescription&gt; 节点中,我有 3 个药物节点:&lt;drug1&gt; , &lt;drug2&gt;&lt;drug3&gt;

有没有一种方法可以使用 XQuery 来计算有多少名为 &lt;drug...&gt; 的节点?

我正在使用下面的 XQuery 但它是错误的:

select prescriptions.query('count(/prescriptions/prescription/main_prescription/drug[*])') as one1 from prescription

【问题讨论】:

    标签: xml xpath xquery


    【解决方案1】:

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

    /*/*/main_prescription
          /*[starts-with(name(), 'drug')
           and
              substring-after(name(), 'drug')
             =
              floor(substring-after(name(), 'drug'))
            ]
    

    这会选择所有的元素

    1. main_prescription 元素的子元素,该元素是文档顶部元素的孙子元素,并且:

    2. 名称以字符串"drug" 开头,"drug" 之后的剩余字符串为整数。

    说明

    1. 正确使用函数starts-with()name()substring-after()

    2. $x 的字符串值表示整数时,表达式floor($x) = $x 的计算结果为true()

    【讨论】:

    • 嗨,Will floor 函数接受字符串作为参数。如果它遇到字符串作为参数会发生什么。
    • @KaipaMSarma:在强类型的 XPath 2.0 中,将非数字参数传递给 floor() 必须导致错误。在非常弱类型的 XPath 1.0 中,floor('3.5') 返回数字 3。floor('Hi') 返回NaN。也就是说,将参数转换为数字,然后应用函数。
    • 好的..现在明白了。我想知道为什么它会为我抛出错误。
    • @KaipaMSarma:在 XPath 2.0 中,您必须使用显式 xs:integer() 将子字符串转换为整数。
    【解决方案2】:

    试试这个。

    'count(/prescriptions/prescription/main_prescription/*[matches(name(), ''^drug[0-9]+$'')])

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

    您可以在下面的链接中探索更多关于 xpath 函数的信息。 http://www.w3schools.com/Xpath/xpath_functions.asp

    【讨论】:

    • 似乎 SQL Server 2008 不同意给出结果的语法:'0-9' 附近的语法不正确。
    • 你必须使用转义字符。我修改了答案
    • @KaipaMSarma:我认为这个表达式没有选择drug99等元素。正确的正则表达式是:^drug[0-9]+$
    猜你喜欢
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 2014-07-30
    • 2016-01-13
    • 2014-08-02
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多