Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

xml - XPath between two elements

I have a Word 2003 XML document that I am trying to search for certain elements in. I have been able to do simple XPath queries to find single elements, but I am having difficulty coming up with a query to search between two elements:

    <w:r>
      <w:fldChar w:fldCharType="begin"/>
    </w:r>
    <w:r>
      <w:instrText> DOCPROPERTY  EvidenceBase  * MERGEFORMAT </w:instrText>
    </w:r>
    <w:r>
      <w:fldChar w:fldCharType="separate"/>
    </w:r>
    <w:r>
      <w:t>EvidenceBase</w:t>
    </w:r>
    <w:r>
      <w:fldChar w:fldCharType="end"/>
    </w:r>

I am searching for the above XML, that has a w:r with a w:fldChar in it which has an attribute of w:fldCharType with value of "begin". It should return every element until it hits a w:r with a w:fldChar in it which has an attribute of w:fldCharType with value of "end".

Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
//w:r[preceding-sibling::w:r[w:fldChar/@w:fldCharType='begin'] and following-sibling::w:r[w:fldChar/@w:fldCharType='end']]

Mind that the prefix w would need to be bound to the proper namespace for the XPath expression namespace context. How this is done depends on how you use the XPath (XSLT, Java, C#...).

Also, this would be more complex if there's multiple, possibly nested "begin" and "end" markers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...