i need to extract the version of a SLD file for GeoServer, which is an XML-based markup language. The version is an attribute of the element StyledLayerDescriptor.
Here is the xml file :
$ cat my_geoserver_sld_file.sld
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd"
xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NamedLayer>
<Name>230_sld_shp_line__230_test_sld_shp_line</Name>
<UserStyle>
<Title>A green line style</Title>
<FeatureTypeStyle>
<Rule>
<Title>green line</Title>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke">#00ff00</CssParameter>
</Stroke>
</LineSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
I would like to set : version="1.0.0"
first the file was opened with de "xmllint --shell" command, in order to use xpath:
$ xmllint --shell my_geoserver_sld_file.sld
/ > xpath *
Object is a Node Set :
Set contains 1 nodes:
1 ELEMENT StyledLayerDescriptor
default namespace href=http://www.opengis.net/sld
namespace ogc href=http://www.opengis.net/ogc
namespace xlink href=http://www.w3.org/1999/xlink
namespace xsi href=http://www.w3.org/2001/XMLSchema-instanc...
ATTRIBUTE version
TEXT
content=1.0.0
ATTRIBUTE schemaLocation
TEXT
content=http://www.opengis.net/sld http://schema...
it should be simple to extract the version, but it fails...
/ > cat //StyledLayerDescriptor/version/text()
/ >
How can i set version in a bash variable ?
Your XPath doesn't work because of the default namespace (http://www.opengis.net/sld
) in your XML.
See this answer for some options on handling default namespaces in xmllint.
Additionally, since the attribute you're trying to select is on the root element, just use /*
in your xpath...
xmllint --xpath "/*/@version" my_geoserver_sld_file.sld
This will return version="1.0.0"
. If you just want the value 1.0.0
, use string()
...
xmllint --xpath "string(/*/@version)" my_geoserver_sld_file.sld