How to convert attributes from XML to values and v

2019-06-05 01:42发布

问题:

here is what I would like to do:

Convert this XML:

<book author="Name" year="2000">Book title</book>

To this XML:

<book><author>Name</author><year>2000</year><value>Book title</value></book>

I would like to do it with xslt or something I can run from bash...

Thanks.

回答1:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:element name="{name()}">
    <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="text()">
  <value>
   <xsl:value-of select="."/>
  </value>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<book author="Name" year="2000">Book title</book>

produces the wanted, correct result:

<book><author>Name</author><year>2000</year><value>Book title</value></book>

Explanation:

  1. The identity rule/template copies every node "as-is".

  2. We override the identity rule with a template matching any attribute. It creates an element whose name is the name of the matched attribute and whose only text-node child is the value of the matched attribute.

  3. Finally, we override the identity rule with a template that matches any text node. It simply outputs this node wrapped in a value parent element.

Do note: The use and overriding of the identity rule is the most fundamental and powerful XSLT design pattern.

I would like to do it with xslt or something I can run from bash...

Most XSLT processors come with a command-line utility that invokes an XSLT transformation from the command line. Read your XSLT processor's documentation.



回答2:

Here's a solution using xmlstarlet & Bash (it doesn't keep the tag order author-year-subvalue though):

(
i=0
xmlstr='<book author="Name" year="2000">Book title</book>'

while IFS="" read -r -d $'\n' xpath; do 
  ((i+=1))
  name="$(printf '%s' "$xmlstr" | xmlstarlet sel -T -t -m "/${xpath}" -v "name(.)")" 
  value="$(printf '%s' "$xmlstr" | xmlstarlet sel -T -t -m "/${xpath}" -v '.')" 
  if [[ $i -eq 1 ]] || [[ "${xpath%/*}" != "${rootxpath}" ]]; then
    rootxpath="${xpath%/*}"
    subvalue="$(printf '%s' "$xmlstr" | xml sel -T -t -m "/${xpath}" -v '..')"
    xmlstr="$(printf '%s' "$xmlstr" | xmlstarlet ed -u "/${xpath}/.." -v "" --subnode "." --type elem -n "$name" -v "$value" --subnode "." --type elem -n subvalue -v "$subvalue" )"
  else
    xmlstr="$(printf '%s' "$xmlstr" | xmlstarlet ed --subnode "/${xpath}/.." --type elem -n "$name" -v "$value")"
  fi
  printf '%s\n' "xpath: $xpath" "name: $name" "value: $value" "subvalue: $subvalue"; echo
done < <(echo "$xmlstr" | xmlstarlet el -a | LC_ALL=C sort -ru | grep -E '/@[^@]+$')  # only get absolute paths that have attributes

# delete all attributes, move subvalues to the last position and format XML output
xmlstr="$(printf '%s\n' "$xmlstr" | xmlstarlet ed -d "//*/@*" -m "//subvalue" "." | tidy -q -xml | xmlstarlet fo -R -o -s 3 -)"

printf '%s\n\n' "$xmlstr"

printf '%s\n' "$xmlstr" | xmlstarlet sel -T -t -m "//book/*[1]" -v "name(.)" -o ': ' -v . -n
printf '%s\n' "$xmlstr" | xmlstarlet sel -T -t -m "//book/*[2]" -v "name(.)" -o ': ' -v . -n
printf '%s\n' "$xmlstr" | xmlstarlet sel -T -t -m "//book/*[3]" -v "name(.)" -o ': ' -v . -n

)

The main output should be:

<book>
   <year>2000</year>
   <author>Name</author>
   <subvalue>Book title</subvalue>
</book> 


标签: xml bash xslt