How I can query this xml file use XPATH expression?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lxmxn

    #1

    How I can query this xml file use XPATH expression?

    <CIM>
    <INSTANCE CLASSNAME="CIM_ DataFile">
    <PROPERTY NAME="FileSize" TYPE="uint64">
    <VALUE>247296 </VALUE>
    </PROPERTY>
    <PROPERTY NAME="Name" TYPE="string">
    <VALUE>e:\tool\ gawk.exe</VALUE>
    </PROPERTY>
    </INSTANCE>
    <INSTANCE CLASSNAME="CIM_ DataFile">
    <PROPERTY NAME="FileSize" TYPE="uint64">
    <VALUE>220</VALUE>
    </PROPERTY>
    <PROPERTY NAME="Name" TYPE="string">
    <VALUE>e:\tool\ i.txt</VALUE>
    </PROPERTY>
    </INSTANCE>
    <INSTANCE CLASSNAME="CIM_ DataFile">
    <PROPERTY NAME="FileSize" TYPE="uint64">
    <VALUE>9</VALUE>
    </PROPERTY>
    <PROPERTY NAME="Name" TYPE="string">
    <VALUE>e:\tool\ run.bat</VALUE>
    </PROPERTY>
    </INSTANCE>
    <INSTANCE CLASSNAME="CIM_ DataFile">
    <PROPERTY NAME="FileSize" TYPE="uint64">
    <VALUE>0</VALUE>
    </PROPERTY>
    <PROPERTY NAME="Name" TYPE="string">
    <VALUE>e:\tool\ temp.xml</VALUE>
    </PROPERTY>
    </INSTANCE>
    </CIM>

    The XML file is above, how can I query the informations (sorted by
    'FileSize') into this format:
    0,e:\tool\temp. xml
    9,e:\tool\run.b at
    220,247296,e:\t ool\i.txt
    247296,e:\tool\ gawk.exe

    I type the following command in CMD, but it doesn't work well.
    xml sel -t -m "//PROPERTY[1]" -v "concat(VALUE,' ')" -o "," -m "//
    PROPERTY[2]" -v "concat(VALUE,' ')" -n myxml.xml
  • Martin Honnen

    #2
    Re: How I can query this xml file use XPATH expression?

    lxmxn wrote:
    The XML file is above, how can I query the informations (sorted by
    'FileSize') into this format:
    0,e:\tool\temp. xml
    9,e:\tool\run.b at
    220,247296,e:\t ool\i.txt
    247296,e:\tool\ gawk.exe
    I don't think a single XPath expression suffices, what you can do is
    write an XSLT stylesheet that sorts and extracts the data:

    <xsl:styleshe et
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output method="text"/>

    <xsl:template match="CIM">
    <xsl:apply-templates select="INSTANC E">
    <xsl:sort select="PROPERT Y[@NAME = 'FileSize']/VALUE"
    data-type="number"/>
    </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="INSTANCE ">
    <xsl:value-of select="concat( PROPERTY[@NAME = 'FileSize']/VALUE,
    ',', PROPERTY[@NAME = 'Name']/VALUE, ' ')"/>
    </xsl:template>

    </xsl:stylesheet>




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • ggrothendieck

      #3
      Re: How I can query this xml file use XPATH expression?

      On May 2, 5:15 am, lxmxn <lxmxn...@gmail .comwrote:
      <CIM>
      <INSTANCE CLASSNAME="CIM_ DataFile">
      <PROPERTY NAME="FileSize" TYPE="uint64">
      <VALUE>247296 </VALUE>
      </PROPERTY>
      <PROPERTY NAME="Name" TYPE="string">
      <VALUE>e:\tool\ gawk.exe</VALUE>
      </PROPERTY>
      </INSTANCE>
      <INSTANCE CLASSNAME="CIM_ DataFile">
      <PROPERTY NAME="FileSize" TYPE="uint64">
      <VALUE>220</VALUE>
      </PROPERTY>
      <PROPERTY NAME="Name" TYPE="string">
      <VALUE>e:\tool\ i.txt</VALUE>
      </PROPERTY>
      </INSTANCE>
      <INSTANCE CLASSNAME="CIM_ DataFile">
      <PROPERTY NAME="FileSize" TYPE="uint64">
      <VALUE>9</VALUE>
      </PROPERTY>
      <PROPERTY NAME="Name" TYPE="string">
      <VALUE>e:\tool\ run.bat</VALUE>
      </PROPERTY>
      </INSTANCE>
      <INSTANCE CLASSNAME="CIM_ DataFile">
      <PROPERTY NAME="FileSize" TYPE="uint64">
      <VALUE>0</VALUE>
      </PROPERTY>
      <PROPERTY NAME="Name" TYPE="string">
      <VALUE>e:\tool\ temp.xml</VALUE>
      </PROPERTY>
      </INSTANCE>
      </CIM>
      >
      The XML file is above, how can I query the informations (sorted by
      'FileSize') into this format:
      0,e:\tool\temp. xml
      9,e:\tool\run.b at
      220,247296,e:\t ool\i.txt
      247296,e:\tool\ gawk.exe
      >
      I type the following command in CMD, but it doesn't work well.
      xml sel -t -m "//PROPERTY[1]" -v "concat(VALUE,' ')" -o "," -m "//
      PROPERTY[2]" -v "concat(VALUE,' ')" -n myxml.xml
      Try matching on INSTANCE rather than PROPERTY:

      xml sel -t -m //INSTANCE -v PROPERTY[1]/VALUE -o , -v PROPERTY[2]/
      VALUE -n myxml.xml

      Comment

      Working...