https://recalll.co/app/?q=xml%20-%20xmlstarlet%20select%20value
The key is to start from the ITEM node, not the identifyer:
Then you can pick out the bits you want:
Great, that works :) Good to see the syntax in an example, I can get forward with this. Thanks!
xml - xmlstarlet select value - Stack Overflow
Building on your first command, you should be able to apply a simple XPath predicate on project to filter it by artifactId element value :
This does not work with the above pom provided. Can I used a shell variable $var instead of 'iwidget' ? However, even without the variable it does not work.
I updated the XML in your post a bit to make it well-formed. Can you try the command above with that exact XML? I tested it here and it worked (xmlstarlet on docker ubuntu 16.04)..
What I am looking for is using a shell variable ($VAR) inplace of "iwidget". This works when I pass the string directly, while I pass $VAR returns nothing.
Regarding the use of variable, you need to wrap it with quotes so that the value will be recognized as string literal : "/x:project[x:artifactId='$var']/x:groupId"
ok works I have been using the double quotes!
xml - xmlstarlet select value based on condition - Stack Overflow
and you will select the x:name with value, ListView, as requested.
thanks! This is what I was looking for!
I'm not clear on what the "x:" is for/represents in the example.
@BillHileman: It is a namespace prefix, defined by OP via -N in xmlstarlet to cover the default namespace in the XML document. See How does XPath deal with XML namespaces?
xml - xmlstarlet select sibling based on text value - Stack Overflow
This XPath will select Value of a State based on its Key equalling state:
Or, in xmlstarlet:
Will return WatchingLiveTV as requested.
xml - Select node by its text value in xmlstarlet - Stack Overflow
This is a very tough call in XPath alone, but it's dead-simple in XSLT.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="UTF-8" />
<xsl:variable name="weatherCond" select="//weather-conditions/value[not(@additive)]" />
<xsl:template match="/">
<xsl:for-each select="//start-valid-time">
<xsl:variable name="myPos" select="position()" />
<xsl:value-of select="." />
<xsl:text>,</xsl:text>
<xsl:value-of select="$weatherCond[position() = $myPos]/@coverage" />
<xsl:value-of select="'
'" />
</xsl:for-each>
</xsl:template>
</xsl:transform>
moreThat said, I suppose you could also simply use two basic xmlstarlet select commands and join their outputs line-wise.
This is excellent. How would I change @coverage to concat both values nodes (if the one containing the additive parameter exists)?
If there can only be one additive parameter per value, just use <xsl:if test="following-sibling::value[1][@additive]"> to check for it and then output it with <xsl:value-of> inside the if. If there can be more, things get a little more complex.
@npostavs I didn't realize that $weatherCond[position() = $myPos] is what I needed and not $weatherCond[position()] . I had a feeling you'd need to store temporary variables. Thank you!
xml - XMLStarlet: concatenate node values from different parents based...
This is a very tough call in XPath alone, but it's dead-simple in XSLT.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="UTF-8" />
<xsl:variable name="weatherCond" select="//weather-conditions/value[not(@additive)]" />
<xsl:template match="/">
<xsl:for-each select="//start-valid-time">
<xsl:variable name="myPos" select="position()" />
<xsl:value-of select="." />
<xsl:text>,</xsl:text>
<xsl:value-of select="$weatherCond[position() = $myPos]/@coverage" />
<xsl:value-of select="'
'" />
</xsl:for-each>
</xsl:template>
</xsl:transform>
moreThat said, I suppose you could also simply use two basic xmlstarlet select commands and join their outputs line-wise.
This is excellent. How would I change @coverage to concat both values nodes (if the one containing the additive parameter exists)?
If there can only be one additive parameter per value, just use <xsl:if test="following-sibling::value[1][@additive]"> to check for it and then output it with <xsl:value-of> inside the if. If there can be more, things get a little more complex.
@npostavs I didn't realize that $weatherCond[position() = $myPos] is what I needed and not $weatherCond[position()] . I had a feeling you'd need to store temporary variables. Thank you!
xml - XMLStarlet: concatenate node values from different parents based...
Use -t and -m to define a template match and then apply another XPath expression with -v.
That's a "short line on the shell", but it's completely unintelligible. Therefore, I would still prefer kjhughes' XSLT solution. Do not sacrifice understandable code in favour of brevity.
You could write a stylesheet that takes an input parameter from the command line, so that you do not have to change the XSLT code if you'd like to retrieve the attribute names of a different element.
As suggested by @npostavs, internally, xmlstarlet makes use of XSLT anyway. You can inspect the XSLT that is generated by replacing -T with -C:
$ xml sel -C -t -m "//mem/@*" -v "name()" -n app.xml
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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="//mem/@*">
<xsl:call-template name="value-of-template">
<xsl:with-param name="select" select="name()"/>
</xsl:call-template>
<xsl:value-of select="' '"/>
</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()>1]">
<xsl:value-of select="' '"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
moreThere are many more options to explore, see the xmlstarlet documentation.
Ah, you got it using XPath interface alone! Very nice. Separately using the -t and -m and -v switches is a great idea. Congrats!
@kjhughes Thanks - still, I'm more the XSLT type of person ;-).
Thanks @MathiasMller, I like this solution better than the XSLT one, although I see your point of it being "completely unintelligible". However, as long as I'm writing it interactively on a shell console just to inspect some XML files (and supposedly know what I'm doing) I would rather use this one liner.
Replace the -T with -C to convert the xmlstarlet command to (hopefully more understandable) XSLT.
@npostavs Thanks for the hint, good to know! The result is surprising (not the stylesheet I would have written), but it makes clear that xmlstarlet uses XSLT anyway. I'll edit my answer.
Retrieving all attribute names of an XML element with xpath on xmlstar...
It should be
...where are you getting the square brackets from?
I've tested the following, and it definitely works:
...this command line works by applying the following XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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="BatchTable/UUThref/@Version">
<xsl:call-template name="value-of-template">
<xsl:with-param name="select" select="."/>
</xsl:call-template>
</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()>1]">
<xsl:value-of select="' '"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
moreEven without square bracket it does not pick the value of Version.
@rkk - Does //UUThref/@Version work? If so, try /UUThref/@Version; this will narrow down where the relevant document root is.
What programming language are you using for the conversion? Some XSLT processors will be picky about having <?xml ..> in your XML file, etc. Do you get any error messages?
@rkk Added an example using XMLStarlet to demonstrate successfully selecting from the document using the given path. If you can't replicate that, we'll need to talk about which tools you're using.
I am using javascript. It does not return any error just nothing gets returned for the Version field (or for any other field like SocketIndex etc)
xslt - selecting an attribute value from an xml file - Stack Overflow
Is that what you needed?
- "// (the double slash means it could be anywhere in the tree)
- -v (requests the value of an element in the current path) and the . represents current element in iteration (you could put the name of the node there but it's generally easier this way) and then the
thanks for help ; but well , that doesn't return the value that i need , for example that doesn't promote me to access to the second item. i found the solution , and i posted my response , and that works.
extracting nodes values with xmlstarlet - Stack Overflow
xmlstarlet is a command line XML Toolkit that can express complex XSLT templates as a short sequence of command line switches.
Suppose we are provided with a well-formed XML document repos.xml
<repositories>
<repositories-item>
<name>hosted-npm</name>
<type>hosted</type>
</repositories-item>
<repositories-item>
<name>proxied-npm</name>
<type>proxied</type>
</repositories-item>
</repositories>
moreIf you run it through an XMLStarlet filter with the following switches
You will get one line of output
- We run the command in the Select mode specified with the sel switch
- We specify the selection template with the -t switch
- We restrict parser to <repositories-item> elements with the //repositories-item template specified with the -m swicth
- We choose only these elements that have "hosted" as the value of type element specified with the -i switch
- We print out the value of the name element, specified with the -v switch.
- After each line of output we print a newline specified with the -n switch.
Here is the equivalent XSLT generated by XMLStarlet
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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="//repositories-item">
<xsl:choose>
<xsl:when test="type="hosted"">
<xsl:call-template name="value-of-template">
<xsl:with-param name="select" select="name"/>
</xsl:call-template>
<xsl:value-of select="' '"/>
</xsl:when>
</xsl:choose>
</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()>1]">
<xsl:value-of select="' '"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
morePer Charles Duffy suggestion it is worth noting that this XSLT specification can be generated with XMLStarlet using the -C option:
This generated XSLT specification can be directly used with xsltproc as
It might be worth showing the OP how to use xsltproc to apply that XSLT, just to be sure they understand they can use this solution on servers without XMLStarlet installed.
bash - Parse xml using shell tools like grep, awk or sed - Stack Overf...
xmlstarlet is a command line XML Toolkit that can express complex XSLT templates as a short sequence of command line switches.
Suppose we are provided with a well-formed XML document repos.xml
<repositories>
<repositories-item>
<name>hosted-npm</name>
<type>hosted</type>
</repositories-item>
<repositories-item>
<name>proxied-npm</name>
<type>proxied</type>
</repositories-item>
</repositories>
moreIf you run it through an XMLStarlet filter with the following switches
You will get one line of output
- We run the command in the Select mode specified with the sel switch
- We specify the selection template with the -t switch
- We restrict parser to <repositories-item> elements with the //repositories-item template specified with the -m swicth
- We choose only these elements that have "hosted" as the value of type element specified with the -i switch
- We print out the value of the name element, specified with the -v switch.
- After each line of output we print a newline specified with the -n switch.
Here is the equivalent XSLT generated by XMLStarlet
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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="//repositories-item">
<xsl:choose>
<xsl:when test="type="hosted"">
<xsl:call-template name="value-of-template">
<xsl:with-param name="select" select="name"/>
</xsl:call-template>
<xsl:value-of select="' '"/>
</xsl:when>
</xsl:choose>
</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()>1]">
<xsl:value-of select="' '"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
morePer Charles Duffy suggestion it is worth noting that this XSLT specification can be generated with XMLStarlet using the -C option:
This generated XSLT specification can be directly used with xsltproc as
It might be worth showing the OP how to use xsltproc to apply that XSLT, just to be sure they understand they can use this solution on servers without XMLStarlet installed.
bash - Parse xml using shell tools like grep, awk or sed - Stack Overf...
I was able to find that node using the following XPath:
To select the Value element and parent/siblings, you could use:
I just re-read your original question. You would like to select the XML based on the value of the Key node. You can do this using the above, but changing the predicate from Value to Key:
@kjhughes has put this into the syntax format you're after.
xml - Select node by its text value in xmlstarlet - Stack Overflow
To select the whole block thus:can:
Or those which somewhere have the string "value1" in the attrib value:
And the 2 restrictions combined:
Beware that when your value=.. attribute should have well defined internal delimiters, to avoid matching an unwanted substring:
and searching with:
does not match that value, but only the real x:ape:y.
Parsing an xml with xmlstarlet - Stack Overflow
standard setup for select with output
This requires a match (-m) at the literal "root" of the document '/root' and for every direct node, in this case defined as an XPath wildcard (*), do something. (FYI - to get funky with xmlstarlet you have to use a little XPath - just make sure to always wrap in (). also handy when using '-i')
This is requesting the value of an element in the current path (-v) and the '.' is for the current element in the iteration. (since we don't know the name)
file name. (yours, not mine)
How to iterate over a xml file by xmlstarlet - Stack Overflow
You can use this command line...
The -t option tells xmlstarlet the options following -t are for templates.
The -v tells xmlstarlet to print the value of the XPath expression.
The -n tells xmlstarlet to print a new line. (Not completely necessary.)
All of this can be found by running xmlstarlet sel --help from the command line.
xml - xmlstarlet Search Attribute - Stack Overflow
I was able to find that node using the following XPath:
To select the Value element and parent/siblings, you could use:
I just re-read your original question. You would like to select the XML based on the value of the Key node. You can do this using the above, but changing the predicate from Value to Key:
@kjhughes has put this into the syntax format you're after.
xml - Select node by its text value in xmlstarlet - Stack Overflow
The key is to start from the ITEM node, not the identifyer:
Then you can pick out the bits you want:
Great, that works :) Good to see the syntax in an example, I can get forward with this. Thanks!
xml - xmlstarlet select value - Stack Overflow
This XPath will select Value of a State based on its Key equalling state:
Or, in xmlstarlet:
Will return WatchingLiveTV as requested.
xml - Select node by its text value in xmlstarlet - Stack Overflow
'인프라보안' 카테고리의 다른 글
ApplicationPoolIndetity (프로세스ID 설정) (0) | 2018.06.16 |
---|---|
보안장비 NTP 연동확인 하기 (0) | 2018.05.29 |
AIX 버전별 eos (0) | 2018.05.29 |
CISCO NEXUS 시리즈 참고 (0) | 2018.05.09 |
최신패치 확인 (0) | 2018.04.06 |