Search code examples
xmlxpathtei

XPath that finds elements with less than 100 characters


I have some XML like this:

<div>
    <sp>
        <speaker>Flores</speaker>
        <l part="y-ns" rend="ws-3">Es muchacho,<ref n="2" /> no te asombre.</l>
    </sp>
    <sp>
        <speaker>Comendador</speaker>
        <l part="y-ns" rend="ws-3">Cuando no sepa mi nombre,</l>
        <l>¿no le sobra el que me dan</l>
        <milestone type="new-stanza" />
        <l part="y-ne"> de Comendador Mayor?</l>
    </sp>
</div>

I want to find the l elements whose text is less than 50 characters. I think the problems are like like the first l, where there is some element in the middle. I tried to concatenate the strings, but I wasn't able to do it right. I have already tried this XPaths:

//l[string-length(./text())< 50]
//l[string-length(.//string(.)) < 50]
//l[string-length(concat(.//text())) < 100]

None works, that is why I came here :) Any idea?


Solution

  • Put point as string-length argument to use text of current l tag

    //l[string-length(.) < 100]