Help with XML/XPATH

Discussion in 'XML' started by baggy, Jul 14, 2005.

  1. baggy

    baggy Guest

    Hi,
    I have the following XML:-

    <X1>
    <Props>
    <Prop xsi:type=\"text\" Name=\"N1\"><Value>TestUser</Value></Prop>
    <Prop xsi:type=\"bool\" Name=\"flag\"><Value>0</Value></Prop>
    <Prop xsi:type=\"int\" Name=\"numemp\"><Value>100</Value></Prop>
    </Props>
    </X1>


    I want to extract just the value for a specific <Prop>, for instance I
    want to be able to just retrieve the Value of the "flag" <Prop>.
    I am thinking of looping through each node and testing for the Name
    attribute but that seems a little complicated. Is there a way to do it
    using XPATH?

    Any help?

    Thanks
    baggy, Jul 14, 2005
    #1
    1. Advertising

  2. baggy

    Dan Guest

    First, I'm assuming your XML looks like this, since what you posted is nt
    well-formed:
    <X1>
    <Props>
    <Prop xsi:type="text" Name="N1"><Value>TestUser</Value></Prop>
    <Prop xsi:type="bool" Name="flag"><Value>0</Value></Prop>
    <Prop xsi:type="int" Name="numemp"><Value>100</Value></Prop>
    </Props>
    </X1>

    To get at the value, the xpath is something like:
    /X1/Props/Prop[@Name='flag']/Value

    The flage value can be input as a variable into the XPATH:
    <xsl:variable name="input">flag</xsl:variable>
    <xsl:value-of select="/X1/Props/Prop[@Name=$input]/Value"/>

    dan


    "baggy" <> wrote in message
    news:...
    > Hi,
    > I have the following XML:-
    >
    > <X1>
    > <Props>
    > <Prop xsi:type=\"text\" Name=\"N1\"><Value>TestUser</Value></Prop>
    > <Prop xsi:type=\"bool\" Name=\"flag\"><Value>0</Value></Prop>
    > <Prop xsi:type=\"int\" Name=\"numemp\"><Value>100</Value></Prop>
    > </Props>
    > </X1>
    >
    >
    > I want to extract just the value for a specific <Prop>, for instance I
    > want to be able to just retrieve the Value of the "flag" <Prop>.
    > I am thinking of looping through each node and testing for the Name
    > attribute but that seems a little complicated. Is there a way to do it
    > using XPATH?
    >
    > Any help?
    >
    > Thanks
    >
    Dan, Jul 14, 2005
    #2
    1. Advertising

  3. baggy

    William Park Guest

    In <comp.text.xml> baggy <> wrote:
    > Hi,
    > I have the following XML:-
    >
    > <X1>
    > <Props>
    > <Prop xsi:type=\"text\" Name=\"N1\"><Value>TestUser</Value></Prop>
    > <Prop xsi:type=\"bool\" Name=\"flag\"><Value>0</Value></Prop>
    > <Prop xsi:type=\"int\" Name=\"numemp\"><Value>100</Value></Prop>
    > </Props>
    > </X1>
    >
    >
    > I want to extract just the value for a specific <Prop>, for instance I
    > want to be able to just retrieve the Value of the "flag" <Prop>. I am
    > thinking of looping through each node and testing for the Name
    > attribute but that seems a little complicated. Is there a way to do it
    > using XPATH?
    >
    > Any help?


    First, fix the quoting of double-quotes. Once you have
    <X1>
    <Props>
    <Prop xsi:type="text" Name="N1"><Value>TestUser</Value></Prop>
    <Prop xsi:type="bool" Name="flag"><Value>0</Value></Prop>
    <Prop xsi:type="int" Name="numemp"><Value>100</Value></Prop>
    </Props>
    </X1>
    then feed it through Bash shell with Expat interface. Something like

    start() # Usage: start tag type=bool Name=flag ...
    {
    case $1 in
    Value) unset value ;;
    esac
    }
    data() # Usage: data text
    {
    case ${XML_ELEMENT_STACK[1]} in
    Value) strcat value "$1" ;;
    esac
    }
    end() # Usage: end tag
    {
    case $1 in
    Value) [ "${XML_ELEMENT_STACK|=Name=flag}" ] && echo "Value=$value" ;;
    easc
    }
    expat -s start -d data -e end < file.xml

    --
    William Park <>, Toronto, Canada
    ThinFlash: Linux thin-client on USB key (flash) drive
    http://home.eol.ca/~parkw/thinflash.html
    BashDiff: Super Bash shell
    http://freshmeat.net/projects/bashdiff/
    William Park, Jul 14, 2005
    #3
  4. baggy

    Guest

    sure, but like they said clean up your XML

    <X1>
    <Props>
    <Prop xsi:type="text" Name="N1"><Value>TestUser</Value></Prop>
    <Prop xsi:type="bool" Name="flag"><Value>0</Value></Prop>
    <Prop xsi:type="int" Name="numemp"><Value>100</Value></Prop>
    </Props>
    </X1>

    using XSL, you can use something like:

    <xsl:value-of select="//Prop[@Name='flag'" />

    I don't know what the full XML looks like so...
    , Jul 16, 2005
    #4
    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. Marvin_123456

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

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,953
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,365
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    505
  4. goog
    Replies:
    0
    Views:
    493
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,607
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page