Search code examples
shellunixbpelxmlstarletxmllint

how can a BPEL variable be put into a shell variable


a BPEL process creates a xml document, a certain XSD file that has xml structure and i want to parse that BPEL variable with xmllint or xmlstarlet with a unix shell commandline command. is that possible at all?

how can i put the BPEL variable into a shell variable , in order to be able to parse it with xmllint for instance?

INPUT:

<?xml version="1.0"?>
<ns:ItemList xmlns:ns="http:///blabla">
    <GenericItem>
        <ns2:LocalItem xmlns:ns2="http:///blabla">
            <ItemSource> </ItemSource>
            <ConcItemSource>
                <name></name>
                <requirements/>
                <strategy/>
            </ConcItemSource>
            <dataFormat/>
            <directory></directory>
            <file/>
        </ns2:LocalItem>
    </GenericItem>
    <GenericItem>
        <ns2:LocalItem xmlns:ns2="http:///blabla">
            <ItemSource>
            </ItemSource>
            <ConcItemSource>
                <name></name>
                <requirements/>
                <strategy/>
            </ConcItemSource>
            <dataFormat/>
            <directory></directory>
            <file/>
        </ns2:LocalItem>
    </GenericItem>
</ns:ItemList>

Solution

  • Using :

    $ cat bpel.xml
    <?xml version="1.0"?>
    <ns:ItemList xmlns:ns="http:///blabla">
        <GenericItem>
            <ns2:LocalItem xmlns:ns2="http:///blabla">
                <ItemSource> </ItemSource>
                <ConcItemSource>
                    <name></name>
                    <requirements/>
                    <strategy/>
                </ConcItemSource>
                <dataFormat/>
                <directory>d1</directory>
                <file/>
            </ns2:LocalItem>
        </GenericItem> 
        <GenericItem>
            <ns2:LocalItem xmlns:ns2="http:///blabla">
                <ItemSource>
                </ItemSource>
                <ConcItemSource>
                    <name></name>
                    <requirements/>
                    <strategy/>
                </ConcItemSource>
                <dataFormat/>
                <directory>d2</directory>
                <file/>
            </ns2:LocalItem>
        </GenericItem>
    </ns:ItemList>
    

    command line :

    $ dir1=$(xmlstarlet sel -t -v '//directory[1]/text()' bpel.xml)
    $ echo "$dir1"
    d1
    

    Using a for loop :

    $ count=$(xmlstarlet sel -t -v 'count(//directory)' bpel.xml)
    $ for ((i=1; i<=count; i++)) {
        xmlstarlet sel -t -v "//directory[$i]/text()" bpel.xml >> newfile
    }
    

    But you can do simply :

    $ xmlstarlet sel -t -v "//directory/text()" bpel.xml >> newfile
    

    xmlstarlet from STDIN :

    command_producing_xml | xmlstarlet sel -t -v "//directory/text()" -