3

I'm trying to construct an XPath argument for use in the program xmllint (used within a Bash shell script) that will return a list of available tags within a tag (while not listing subtags).

Here's the sort of XML I have:

<functionInformation>
    <class>
        setup
    </class>
    <description>
        This is a natural language description of this function.
    </description>
    <prerequisiteFunctions>
        myFunction1
        myFunction2
    </prerequisiteFunctions>
    <prerequisitePrograms>
        myProgram1
        myProgram2
    </prerequisitePrograms>
</functionInformation>

This XML is stored in the Bash variable functionInformation.

The output that I would like to have when using xmllint on this XML is the following:

class
description
prerequisiteFunctions
prerequisitePrograms

I should note that I would like the tags returned in a non recursive way (I do not want all available tags or subtags listed).

I can access information in tags using xmllint in a way such as the following:

descriptionFunctionInformation="$(echo "${functionInformation}"\
                | xmllint --xpath '/functionInformation/description/text()' -\
                | xargs -i echo -n "{}")"

Could you point me in the right direction on how I may build an XPath (or something similar) to return the information I need?

4

1 回答 1

1

您可以使用xmlstarlet

xmlstarlet sel -t -m '/*/*' -v 'concat(name(.)," ")' < xmlfile
于 2013-02-08T21:12:22.960 回答