2

I'm using Java and import javax.xml.xpath.* package.

I'm a beginner with XPATH and I can not recover a value based on another value.

Here my .xml file

<lom:lom xmlns:lom="http://ltsc.ieee.org/xsd/LOM" xmlns:lomfr="http://www.lom-fr.fr/xsd/LOMFR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <lom:general>
          ...
          <lom:title>
              <lom:string language="fre">Analyse financière bilan : Un exemple d'approche financière par la méthode des ratios - la centrale de bilans de la Banque de France</lom:string>
         </lom:title>
         ...
    </lom:general>

    <lom:lifeCycle>
        ...
        <lom:contribute>
            <lom:role>
                <lom:source>LOMv1.0</lom:source>
                <lom:value>author</lom:value>
            </lom:role>
            <lom:entity>BEGIN:VCARD VERSION:3.0 N:GARROT;Thierry;;; FN:Thierry GARROT EMAIL;TYPE=INTERNET:[email protected] ORG:Université de Nice END:VCARD</lom:entity>
            <lom:date>
                <lom:dateTime>2009-10-07</lom:dateTime>
            </lom:date>
        </lom:contribute>
        <lom:contribute>
             <lom:role>
                 <lom:source>LOMv1.0</lom:source>
                 <lom:value>instructional designer</lom:value>
             </lom:role>
             <lom:entity>BEGIN:VCARD VERSION:3.0 N:CASANOVA;Gérard;;; FN:Gérard CASANOVA EMAIL;TYPE=INTERNET:[email protected] ORG:Université de Lorraine END:VCARD</lom:entity>
             <lom:date>
                 <lom:dateTime>2009-10-07</lom:dateTime>
             </lom:date>
        </lom:contribute>
        <lom:contribute>
             ...
        </lom:contribute>
        ...
    </lom:lifeStyle>
 </lom>

How can I get lom:entity value only if lom:value value is author?

lom:entity is a VCARD but I think that it's a problem because I have an algorithm to get author fullname.

Example:

To get lom:title I use : //*[local-name()='title']/*[local-name()='string']/text().

Thank's for help!

2 Answers 2

2

The XPath expression you are looking for is (expecting a proper namespace handling):

/lom:lom/lom:lifeCycle/lom:contribute[lom:role/lom:value = 'author']/lom:entity

This should give you the desired content.

A namespace ignoring variant of the above XPath expression is

/*[local-name()='lom']/*[local-name()='lifeCycle']/*[local-name()='contribute'][*[local-name()='role']/*[local-name()='value'] = 'author']/*[local-name()='entity']
Sign up to request clarification or add additional context in comments.

5 Comments

@N.Lamblin: My presumption was a proper namespace handling. I can only guess that this is the problem you have.
Possible ... But in this case why the expression to get the title is working? :/
@N.Lamblin: Because with you expression *[local-name()='title'] and *[local-name()='string'] you were skipping any namespaces. I'm gonna add a namespace-ignorant version of my expression.
@N.Lamblin: I added a (somewhat ridiculous) version of my answer. It does work, but the previous version was far more elegant. Sorry to mention that.
With the version which ignore namespace, it's working. Thank's a lot! I will try to understand why the first version is not works ... :)
1

How can I get lom:entity value only if lom:value value is author?

xpath expression:

//lom:entity[parent::lom:contribute/lom:role/lom:value="author"]/text()

2 Comments

Like zx485 I get an empty result :/
@N.Lamblin, verify your input structure with XML validator beforehand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.