Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    help with xml queries with x path

    Hey, I'm new to XML and and need some help I'm trying to learn the basics of xpath and cant get the hang of queries.
    I have the following code about about transactions which is from the XML how to program book but am rely struggling with the answers.

    Code:
    <?xml version = "1.0"?>
    <transactions>
    <transaction date = "05/22/2000" id = "0122">
    <from account = "100392"/>
    <to account = "203921"/>
    <amount currency = "USD">15</amount>
    </transaction>
    <transaction date = "06/01/2000" id = "0129">
    <from account = "203921"/>
    <to account = "877521"/>
    <amount currency = "USD">4800</amount>
    </transaction>
    <transaction date = "06/01/2000" id = "0130">
    <from account = "100392"/>
    <to account = "992031"/>
    <amount currency = "YEN">7000</amount>
    </transaction>
    <transaction date = "06/10/2000" id = "0152">
    <from account = "992031"/>
    <to account = "100392"/>
    <amount currency = "USD">402.53</amount>
    </transaction>
    <transaction date = "06/22/2000" id = "0188">
    <from account = "100392"/>
    <to account = "203921"/>
    <amount currency = "USD">10000</amount>
    </transaction>
    <transaction date = "07/12/2000" id = "0200">
    <from account = "100392"/>
    <to account = "039211"/>
    <amount currency = "NTD">3000</amount>
    </transaction>
    <transaction date = "07/26/2000" id = "0211">
    <from account = "203921"/>
    <to account = "100392"/>
    <amount currency = "USD">400</amount>
    </transaction>
    <transaction date = "08/05/2000" id = "0225">
    <from account = "039211"/>
    <to account = "203921"/>
    <amount currency = "USD">150</amount>
    </transaction>
    <transaction date = "09/03/2000" id = "0293">
    <from account = "100392"/>
    <to account = "039211"/>
    <amount currency = "NTD">200000</amount>
    </transaction></transactions>

    The questions i need help on are:

    What is the XPath expression that selects all transaction elements with attribute
    date having values between 06/01/2000 and 07/30/2000, inclusive?

    (IM rely struggling on this one)

    What is the XPath expression that calculates the total value of transactions in account
    203921?

    What is the XPath expression to determine the average amount transferred to account
    203921?

    any help/tips/answers to these questions will be apprciated as think i just need the soloutions rely to learn how its done myself.

    Thanks a bunch in adavance
    Last edited by mattys1; 11-15-2008 at 05:59 PM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    hi,

    and welcome to the forum.
    Please put your code between [ code] and [ /code] tags. You can edit your previous post to do that. Thank you.

    Quote Originally Posted by mattys1 View Post
    Hey, I'm new to XML and and need some help I'm trying to learn the basics of xpath and cant get the hang of queries.
    I have the following code about about transactions which is from the XML how to program book but am rely struggling with the answers.

    Code:
    <?xml version = "1.0"?>
    <transactions>
    <transaction date = "05/22/2000" id = "0122">
    <from account = "100392"/>
    <to account = "203921"/>
    <amount currency = "USD">15</amount>
    </transaction>
    <transaction date = "06/01/2000" id = "0129">
    <from account = "203921"/>
    <to account = "877521"/>
    <amount currency = "USD">4800</amount>
    </transaction>
    <transaction date = "06/01/2000" id = "0130">
    <from account = "100392"/>
    <to account = "992031"/>
    <amount currency = "YEN">7000</amount>
    </transaction>
    <transaction date = "06/10/2000" id = "0152">
    <from account = "992031"/>
    <to account = "100392"/>
    <amount currency = "USD">402.53</amount>
    </transaction>
    <transaction date = "06/22/2000" id = "0188">
    <from account = "100392"/>
    <to account = "203921"/>
    <amount currency = "USD">10000</amount>
    </transaction>
    <transaction date = "07/12/2000" id = "0200">
    <from account = "100392"/>
    <to account = "039211"/>
    <amount currency = "NTD">3000</amount>
    </transaction>
    <transaction date = "07/26/2000" id = "0211">
    <from account = "203921"/>
    <to account = "100392"/>
    <amount currency = "USD">400</amount>
    </transaction>
    <transaction date = "08/05/2000" id = "0225">
    <from account = "039211"/>
    <to account = "203921"/>
    <amount currency = "USD">150</amount>
    </transaction>
    <transaction date = "09/03/2000" id = "0293">
    <from account = "100392"/>
    <to account = "039211"/>
    <amount currency = "NTD">200000</amount>
    </transaction></transactions>
    The questions i need help on are:

    What is the XPath expression that selects all transaction elements with attribute
    date having values between 06/01/2000 and 07/30/2000, inclusive?
    this can be solved using exslt, date and time module:

    http://www.exslt.org/date/index.html

    can use also function module to write your own function:

    http://www.exslt.org/func/index.html

    or you can use substring, substring-after, substring-before:

    http://zvon.org/xxl/XSLTreference/Output/index.html

    using one of this method you can write a condition which return true and test
    using a xpath like that:

    Code:
    /transactions/transaction[your-condition(@date,start-date,end-date)]
    (IM rely struggling on this one)

    What is the XPath expression that calculates the total value of transactions in account
    203921?
    similar way, use xpath to extract a node set with values and write a function to calculate sum.

    What is the XPath expression to determine the average amount transferred to account
    203921?
    similar way with previous, use value from previous but use count(value-node-set) to divide the sum and compute average.

    To have a working solution you must see what is implemented in your xslt processor and what not. Post question and problems if you have, please.

    best regards

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    using one of this method you can write a condition which return true and test
    using a xpath like that:

    Code:
    /transactions/transaction[your-condition(@date,start-date,end-date)]
    im not entirly shaw how to write a condition to be honest im fairly new to xml and am just rely stumpted on these questions i have downloaded a few xml tools such as stylus studio to help me with the questions but still cant create the queries needed to answer them.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by mattys1 View Post
    using one of this method you can write a condition which return true and test
    using a xpath like that:

    Code:
    /transactions/transaction[your-condition(@date,start-date,end-date)]
    im not entirly shaw how to write a condition to be honest im fairly new to xml and am just rely stumpted on these questions i have downloaded a few xml tools such as stylus studio to help me with the questions but still cant create the queries needed to answer them.
    you start with a complicated thing even for a advanced user,
    there are examples on each sections:
    for example at functions, func:functions a the end of the page:

    http://exslt.org/func/elements/function/index.html

    try to start from there, write your own and if you are stuck, post, I will try to help you.

    regards

  • Users who have thanked oesxyl for this post:

    mattys1 (11-17-2008)

  • #5
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for all ur help, do i use the fn:avg((arg,arg,...)) and fn:sum(arg,arg,...) functions to work these questions out.
    What is the XPath expression that calculates the total value of transactions in account
    203921?

    What is the XPath expression to determine the average amount transferred to account
    203921?

    Would you be able to give me a example to how these would work.

    Thanks for all ur help

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by mattys1 View Post
    Thanks for all ur help, do i use the fn:avg((arg,arg,...)) and fn:sum(arg,arg,...) functions to work these questions out.
    I don't know this functions but if they work in your processor( test them), is ok.

    What is the XPath expression that calculates the total value of transactions in account
    203921?

    What is the XPath expression to determine the average amount transferred to account
    203921?

    Would you be able to give me a example to how these would work.

    Thanks for all ur help
    I prepare a example for another question from your first post:
    What is the XPath expression that selects all transaction elements with attribute
    date having values between 06/01/2000 and 07/30/2000, inclusive?

    is tested with xml from your first post but I don't check if the restriction inclusive/exclusive dates is ok. I intend only to provide a working example.

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:func="http://exslt.org/functions"
                    extension-element-prefixes="func">
    
      <xsl:variable name="startdate"
                    select="'06/01/2000'"/>
      <xsl:variable name="enddate"
                    select="'07/30/2000'"/>
    
      <xsl:output method="xml"
                  indent="yes"/>
    
      <xsl:template match="/">
        <div>
          <p>between dates</p>
          <xsl:apply-templates select="transactions/transaction[func:between-dates(@date,$startdate,$enddate)]">
            <xsl:sort data-type="text" select="@date"/>
          </xsl:apply-templates>
          <p>outside dates</p>
          <xsl:apply-templates select="transactions/transaction[not(func:between-dates(@date,$startdate,$enddate))]">
            <xsl:sort data-type="text" select="@date"/>
          </xsl:apply-templates>
        </div>
      </xsl:template>
    
      <xsl:template match="transaction">
        <p>
          <xsl:value-of select="func:between-dates(@date,$startdate,$enddate)"/>
          <br/>
          <xsl:value-of select="@date"/>
        </p>
      </xsl:template>
    
      <func:function name="func:getyear">
        <xsl:param name="adate"/>
        <func:result select="number(substring-after(substring-after($adate,'/'),'/'))"/>
      </func:function>
    
      <func:function name="func:getday">
        <xsl:param name="adate"/>
        <func:result select="number(substring-before(substring-after($adate,'/'),'/'))"/>
      </func:function>
    
      <func:function name="func:getmonth">
        <xsl:param name="adate"/>
        <func:result select="number(substring-before($adate,'/'))"/>
      </func:function>
    
      <func:function name="func:date-before">
        <xsl:param name="x"/>
        <xsl:param name="y"/>
        <xsl:variable name="yx" select="func:getyear($x)"/>
        <xsl:variable name="yy" select="func:getyear($y)"/>
        <xsl:variable name="mx" select="func:getmonth($x)"/>
        <xsl:variable name="my" select="func:getmonth($y)"/>
        <xsl:variable name="dx" select="func:getday($x)"/>
        <xsl:variable name="dy" select="func:getday($y)"/>
        <func:result select="$yx &lt; $yy or ($yx = $yy and ($mx &lt; $my or ($mx = $my and $dx &lt;= $dy)))"/>
      </func:function>
    
      <func:function name="func:between-dates">
        <xsl:param name="x"/>
        <xsl:param name="y"/>
        <xsl:param name="z"/>
        <func:result select="func:date-before($y,$x) and func:date-before($x,$z)"/>
      </func:function>
    
    </xsl:stylesheet>
    best regards

  • Users who have thanked oesxyl for this post:

    mattys1 (11-18-2008)

  • #7
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks for all ur help i have working querys for all the above questions now.
    Thanks again

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by mattys1 View Post
    thanks for all ur help i have working querys for all the above questions now.
    Thanks again
    you are welcome,

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •