How I can query this xml file use XPATH expression?

Discussion in 'XML' started by lxmxn, May 2, 2008.

  1. lxmxn

    lxmxn Guest

    <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.bat
    220,247296,e:\tool\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
     
    lxmxn, May 2, 2008
    #1
    1. Advertising

  2. 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.bat
    > 220,247296,e:\tool\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:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:eek:utput method="text"/>

    <xsl:template match="CIM">
    <xsl:apply-templates select="INSTANCE">
    <xsl:sort select="PROPERTY[@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/
     
    Martin Honnen, May 2, 2008
    #2
    1. Advertising

  3. On May 2, 5:15 am, lxmxn <> wrote:
    > <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.bat
    > 220,247296,e:\tool\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
     
    ggrothendieck, May 3, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jake
    Replies:
    0
    Views:
    349
  2. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,976
    jan V
    Jul 29, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,973
    vforvikash
    Dec 29, 2008
  4. David Gordon

    xpath query query

    David Gordon, May 18, 2005, in forum: XML
    Replies:
    2
    Views:
    796
    David Gordon
    May 18, 2005
  5. xerj
    Replies:
    2
    Views:
    394
Loading...

Share This Page