Skip to main content
added 478 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

With parameters taken from shell variables:

unit=photoncannon
name=cooldown
xmlstrlet sel -t \
    --var unit="'$unit'" \
    --var name="'$name'" \
    -v '//entry[@unit=$unit and name=$name]/value' -nl file

(Note the specific quoting used; the internal variables $unit and $name must contain XML-encoded strings, which means they need to have explicit quotes added.)

You could obviously use any other XML parser to do the same thing. The following uses xq (from here), which is an XML parser sitting on top of jq, the JSON parser, which means it first translates your XML document into JSON, and then calls jq to parse the resulting data:

unit=photoncannon
name=cooldown
xq -r \
    --arg unit "$unit" \
    --arg name "$name" \
    '.record.entry[] | select(."@unit" == "photoncannon"$unit and .name == "cooldown"$name).value' file

You could obviously use any other XML parser to do the same thing. The following uses xq (from here), which is an XML parser sitting on top of jq, the JSON parser, which means it first translates your XML document into JSON, and then calls jq to parse the resulting data:

xq -r '.record.entry[] | select(."@unit" == "photoncannon" and .name == "cooldown").value' file

With parameters taken from shell variables:

unit=photoncannon
name=cooldown
xmlstrlet sel -t \
    --var unit="'$unit'" \
    --var name="'$name'" \
    -v '//entry[@unit=$unit and name=$name]/value' -nl file

(Note the specific quoting used; the internal variables $unit and $name must contain XML-encoded strings, which means they need to have explicit quotes added.)

You could obviously use any other XML parser to do the same thing. The following uses xq (from here), which is an XML parser sitting on top of jq, the JSON parser, which means it first translates your XML document into JSON, and then calls jq to parse the resulting data:

unit=photoncannon
name=cooldown
xq -r \
    --arg unit "$unit" \
    --arg name "$name" \
    '.record.entry[] | select(."@unit" == $unit and .name == $name).value' file
added 478 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

To transform you string to valid XML, which is what I presume you want to do, youtransform your string into valid XML. You will not only need to remove the bdy strings, but you also need to fix the entry tag and its unit attribute (currently written together as entryunit), add the missing <record> start tag at the start, and remove the trl string at the very end.

The sed command first deletes all the bdy strings. We match one or two digits after the substring bdy using [0-9]\{2,3\}, which literally means "two or three digits between 0 and 9".

The entryunit string is corrected into entry unit using a straight-forwardstraightforward substitution.

TheWe delete the trl string and the trailing string of digits at the end is deleted usingwith the last substitution.

ThisThe above outputs the value of the value node belonging to the entry whose unit attribute is photoncannon and whose name child-node's node's value is cooldown. In other words, it gets the cool-down value for the photon cannon.

You could obviously use any other XML parser to do the same thing. The following uses xq (from here), which is an XML parser sitting on top of jq, the JSON parser, which means it first translates your XML document inoeinto JSON, and then calls jq to parse the resulting data:

To transform you string to valid XML, which is what I presume you want to do, you will need to remove the bdy strings, but you also need to fix the entry tag and its unit attribute (currently written together as entryunit), add the missing <record> start tag at the start, and remove the trl string at the very end.

The sed command first deletes all the bdy strings. We match one or two digits after the substring bdy using [0-9]\{2,3\} which literally means "two or three digits between 0 and 9".

The entryunit string is corrected into entry unit using a straight-forward substitution.

The trl string and the trailing string of digits at the end is deleted using the last substitution.

This outputs the value of the value node belonging to the entry whose unit attribute is photoncannon and whose name child-node's value is cooldown. In other words, it gets the cool-down value for the photon cannon.

You could obviously use any other XML parser to do the same thing. The following uses xq (from here), which is an XML parser sitting on top of jq, the JSON parser, which means it first translates your XML document inoe JSON, and then calls jq to parse the resulting data:

I presume you want to transform your string into valid XML. You will not only need to remove the bdy strings but also need to fix the entry tag and its unit attribute (currently written together as entryunit), add the missing <record> start tag at the start and remove the trl string at the very end.

The sed command first deletes all the bdy strings. We match one or two digits after the substring bdy using [0-9]\{2,3\}, which means "two or three digits between 0 and 9".

The entryunit string is corrected into entry unit using a straightforward substitution.

We delete the trl string and the trailing string of digits with the last substitution.

The above outputs the value of the value node belonging to the entry whose unit attribute is photoncannon and whose name child node's value is cooldown. In other words, it gets the cool-down value for the photon cannon.

You could obviously use any other XML parser to do the same thing. The following uses xq (from here), which is an XML parser sitting on top of jq, the JSON parser, which means it first translates your XML document into JSON, and then calls jq to parse the resulting data:

added 478 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Regardless of whether you reformat the resulting XML document with xmlstarlet or not later, you may extract data using the tool like so:

xmlstarlet sel -t -v '//entry[@unit="photoncannon" and name="cooldown"]/value' -nl file

This outputs the value of the value node belonging to the entry whose unit attribute is photoncannon and whose name child-node's value is cooldown. In other words, it gets the cool-down value for the photon cannon.

You could obviously use any other XML parser to do the same thing. The following uses xq (from here), which is an XML parser sitting on top of jq, the JSON parser, which means it first translates your XML document inoe JSON, and then calls jq to parse the resulting data:

xq -r '.record.entry[] | select(."@unit" == "photoncannon" and .name == "cooldown").value' file

Regardless of whether you reformat the resulting XML document with xmlstarlet or not later, you may extract data using the tool like so:

xmlstarlet sel -t -v '//entry[@unit="photoncannon" and name="cooldown"]/value' -nl file

This outputs the value of the value node belonging to the entry whose unit attribute is photoncannon and whose name child-node's value is cooldown. In other words, it gets the cool-down value for the photon cannon.

You could obviously use any other XML parser to do the same thing. The following uses xq (from here), which is an XML parser sitting on top of jq, the JSON parser, which means it first translates your XML document inoe JSON, and then calls jq to parse the resulting data:

xq -r '.record.entry[] | select(."@unit" == "photoncannon" and .name == "cooldown").value' file
added 514 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading