简体   繁体   中英

Obtain value from XML element using XML parser and assign it to a variable

I have the following XML:

        <?xml version="1.0" encoding="UTF-8"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
             xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <groupId>BundleResourceDeploy</groupId>
        <artifactId>Project3</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>sbar</packaging>
        <description></description>
</project>

Also I have the following XSLT stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://maven.apache.org/POM/4.0.0" exclude-result-prefixes="ns">

 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
 <xsl:template match="ns:project">


  <xsl:element name="ns:groupId">
    <xsl:value-of select="ns:groupId"/>
  </xsl:element>

  <xsl:element name="ns:artifactId">
    <xsl:value-of select="ns:artifactId"/>
  </xsl:element>

  <xsl:element name="ns:version">
    <xsl:value-of select="ns:version"/>
  </xsl:element>

  <xsl:element name="ns:packaging">
    <xsl:value-of select="ns:packaging"/>
  </xsl:element>

 </xsl:template>
</xsl:stylesheet>

Executing the command: xsltproc "stylesheet" "xml_file" > output.xml

The output (content of output.xml file) is:

<ns:groupId xmlns:ns="http://maven.apache.org/POM/4.0.0">BundleResourceDeploy</ns:groupId><ns:artifactId xmlns:ns="http://maven.apache.org/POM/4.0.0">Project2</ns:artifactId><ns:version xmlns:ns="http://maven.apache.org/POM/4.0.0">1.0-SNAPSHOT</ns:version><ns:packaging xmlns:ns="http://maven.apache.org/POM/4.0.0">sbar</ns:packaging>

What I want to achieve is: In a shell script I want to obtain the value of the element "groupId", which would be "BundleResourceDeploy", and assign it to a variable.

Could you help me out with a way on how to achieve this?

Personally, I'd use XMLStarlet:

#!/bin/bash
#      ^^^^-- NOT /bin/sh; process substitution is an extension in ksh and bash
#             $'' literal syntax is an extension as well, likewise read -d

IFS=$'\n' read -r -d '' groupId artifactId version packaging < <(
  xmlstarlet sel \
    -N ns='http://maven.apache.org/POM/4.0.0' \
    -t -m /ns:project \
    -v ns:groupId -n \
    -v ns:artifactId -n \
    -v ns:version -n \
    -v ns:packaging -n \
    <pom.xml && printf '\0'
)

echo "pom.xml is for $groupId/$artifactId, version $version, with packaging $packaging"

However, what the xmlstarlet command in that script actually does is entirely equivalent to a slightly different XSLT template, which you can ask XMLStarlet to generate for you by passing the -C argument:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://maven.apache.org/POM/4.0.0" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
  <xsl:output omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="/">
    <xsl:for-each select="/ns:project">
      <xsl:call-template name="value-of-template">
        <xsl:with-param name="select" select="ns:groupId"/>
      </xsl:call-template>
      <xsl:value-of select="'&#10;'"/>
      <xsl:call-template name="value-of-template">
        <xsl:with-param name="select" select="ns:artifactId"/>
      </xsl:call-template>
      <xsl:value-of select="'&#10;'"/>
      <xsl:call-template name="value-of-template">
        <xsl:with-param name="select" select="ns:version"/>
      </xsl:call-template>
      <xsl:value-of select="'&#10;'"/>
      <xsl:call-template name="value-of-template">
        <xsl:with-param name="select" select="ns:packaging"/>
      </xsl:call-template>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="value-of-template">
    <xsl:param name="select"/>
    <xsl:value-of select="$select"/>
    <xsl:for-each select="exslt:node-set($select)[position()&gt;1]">
      <xsl:value-of select="'&#10;'"/>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Note that we're not creating elements in our output here at all, but rather generating a textual stream delimited by newlines. (We'd need to adjust our approach if newlines could appear in our values, but that's not the case in a Maven POM).


If you were using the stylesheet I gave above, the exact script would be:

#!/bin/bash
IFS=$'\n' read -r -d '' groupId artifactId version packaging \
   < <(xsltproc stylesheet.xslt pom.xml) && printf '\0')

echo "pom.xml is for $groupId/$artifactId, version $version, with packaging $packaging"

Below a better option using xmllint . The example is a shell script which returns the value of the <version/> element:

#!/bin/bash
echo 'setns ns=http://maven.apache.org/POM/4.0.0
cat /ns:project/ns:version/text()' | \
xmllint --shell pom.xml | egrep -v '^(/ >| -----)'

I managed to achieve it by working directly on the source XML file instead of working with xsltproc. In my shell script I created the following variable:

GROUPID="$(grep -Po '(?<=)\\w+(?=)' name_of_xml_file_here"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM