简体   繁体   中英

How to get the number of elements in XPath

I have started with Schematron and XPath. In Schematron document I to provide context for validating m business rule so I am providing section/body/p. As I have provided p tag in context then How can I query to count total no of p tags? This is the XML file

  <section>
    <body>
        <p>
            <t>Document Body</t>
        </p>
        <p>
            <t>Document Body</t>
        </p>
        <p>
            <t>Document Body</t>
        </p>
    </body>
</section>

And this is how I am querying

 <sch:pattern name="ELEMENT_LIMIT">
    <sch:rule context="section/body/p" >
        <sch:assert test="count(.//following-sibling::p) &lt;=2">more than 1 sibling</sch:assert>
    </sch:rule> 
 </sch:pattern> 

but this is not working.

This reports, if a <p> element has less than 2 following-sibling <p> elements.

<sch:pattern id="too-many-following-siblings">
    <sch:rule context="p">
        <sch:report test="count(following-sibling::p) lt 2">
            Less than 2 following-sibling &lt;p&gt; elements.
        </sch:report>
    </sch:rule>
</sch:pattern>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM