2

The sample file:

<settings
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
    <server>
        <id>fake</id>
        <username>username2</username>
        <password>aa2</password>
    </server>
    <server>
        <id>fake3</id>
        <username>username3</username>
        <password>aa3</password>
    </server>
    <server>
        <id>gitlab-company-name</id>
        <configuration>
            <httpHeaders>
                <property>
                    <name>Private-Token</name>
                    <value>glpat-faketokenvalue-E</value>
                </property>
            </httpHeaders>
        </configuration>
    </server>
</servers>
<mirrors>
[parially cut]
</mirrors>

For now I would like to return value of the settings -> servers -> server (where id = gitlab-company-name) -> configuration -> httpHeaders -> property -> name and value - separately.

The best what I figured out is:

xmlstarlet sel -t -m '//_:server[_:id="gitlab-company-name"]' -v '_:configuration' -n ${HOME}/.m2/settings.xml

but that returns all the spaces until the result as well, (they are truncated by the StackExchange formatting):

                    Private-Token
                    glpat-faketokenvalue-E

I piped that to sed and head/tail but it's a dirty workaround.

Anyone can help me to resolve that issue? Thank you in advance.

Destroy666
  • 12,350

1 Answers1

2

You can for example use:

xmlstarlet sel -t -m '//_:server[_:id="gitlab-company-name"]' -v '//_:name' -n -v "//_:value" -n ${HOME}/.m2/settings.xml

What I did there was a split between the two nested values (name, value) and there's a newline (-n switch) between them.

Result:

Private-Token
glpat-faketokenvalue-E

Or you could alternatively use concat inside of ANSI C string ($'') to parse newlines, assuming your shell supports that syntax:

xmlstarlet sel -t -m '//_:server[_:id="gitlab-company-name"]' -v $'concat(//_:name, "\n", //_:value, "\n")' ${HOME}/.m2/settings.xml
Destroy666
  • 12,350