1
votes

I have the following 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>[email protected]</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>

In <main_prescription> node I have 3 nodes for drugs: <drug1> , <drug2> and <drug3>.

Is there a way to count how many nodes name <drug...> are there using XQuery?

I am using the XQuery below but it is wrong:

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

2 Answers

2
votes

The wanted nodes can be selected even with an XPath 1.0 expression:

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

This selects all elements that:

  1. Are children of a main_prescription element that is a grand-child of the top element of the document and:

  2. Have a name starting with the string "drug" and the remaining string after "drug" is an integer.

Explanation:

  1. Proper use of the functions starts-with(), name() and substring-after().

  2. Use of the fact that the expression floor($x) = $x evaluates to true() exactly when the string value of $x represents an integer.

0
votes

Try this.

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

Here we are selecting all nodes which are having node names as 'drug' and ending with an integer.

You can explore more on xpath functions on the below link. http://www.w3schools.com/Xpath/xpath_functions.asp