[Python] How to extract the context of an XML element with lxml

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I want to extract the citation context of PubMed Central papers, given the following data structure (Journal Article Tag Suite, JATS):

    <p>
    This is a sentence.
    This is a citing sentence [<xref ref-type="bibr" rid="CR1">1</xref>].
    This is another sentence
    </p>


    A real example file: https://www.dropbox.com/s/u4g1sisil33wnhu/PMC1914234.xml?dl=0

    I want to extract "This is a citing sentence" as the context of reference with rid CR1, and do not extract the previous and the following sentences.

    I am able to locate the paragraph that contains the xref tag, but I don't know how to extract the correct sentence only. The XPath command string() or text() of this paragraph only return texts without structural information (e.g. tag) so I have difficulties in locating the exact sentence.

    Is there a solution to solve this problem?

    EDIT: I think I need an XPath command that can extract texts with structural information, instead of using string() and text() on its parent element.

    Continue reading...

Compartilhe esta Página