Page view counter

Need help with XPath Query

Last post 12-06-2007 10:14 PM by Samu Zhang - MSFT. 7 replies.

Sort Posts:

  • Need help with XPath Query

    12-03-2007, 8:46 PM
    • Loading...
    • ljp007
    • Joined on 06-18-2002, 7:40 PM
    • Posts 155
    • Points 507

    I'm trying to construct and Xpath query that will only show me the TWO most recent 'Industry Study' documents.

    Im using the query below, but cant figure out the part of the query that would ORDER BY DATE and Select the TOP 2. The T-Sql equivalent would be: "Select Top 2 Title Where Type = 'Case Study' Order By PublishDate (or something like that).

    //Collection/Content[(Html/root/DocInfo/Type = 'Case Study')].

     <Collection> 

     <Content>
    <Html>
    <root>
    <
    DocInfo>
    <Title>Document Title</Title>
    <Type>Industry Study</Type>
    <PublishDate>2003-07-01</PublishDate>
    <NumberPages>05</NumberPages>
    </DocInfo>
    </root>
    </
    Html
    </Content> <Content>
    <Html>
    <root>
    <DocInfo>
    <Title>Document Title</Title>
    <Type>Industry Study</Type>
    <PublishDate>2003-03-01</PublishDate>
    <NumberPages>02</NumberPages>
    </DocInfo>
    </root>
    </
    Html
    </Content>

     <Content>
    <Html>
    <root>
    <DocInfo>
    <Title>Document Title</Title>
    <Type>Industry Study</Type>
    <PublishDate>2003-06-01</PublishDate>
    <NumberPages>15</NumberPages>
    </DocInfo>
    </root>
    </
    Html
    </Content>

    <Content>
    <Html>
    <root>
    <DocInfo>
    <Title>Document Title</Title>
    <Type>Case Study</Type>
    <PublishDate>2004-11-01</PublishDate>
    <NumberPages>12</NumberPages>
    </DocInfo>
    </root>
    </
    Html
    </Content>

     </Collection> 

     Thanks for any help

  • Re: Need help with XPath Query

    12-04-2007, 10:59 AM
    • Loading...
    • pgfearo
    • Joined on 09-26-2007, 7:34 AM
    • Posts 5
    • Points 40

    I'm afraid XPath 1.0 can't sort nodesets on its own, it always provides nodesets in document order.

    Alternatives would be to use XPath 2.0  (Requires Saxon.NET), Linq to XML (requires .NET 3.5) or XSLT 1.0/2.0.

    Another choice would be to iterate through the returned nodeset using the XPathNodeIterator and perform the sort then.

    Phil Fearon

    http://www.sketchpath.com 

  • Re: Need help with XPath Query

    12-04-2007, 1:34 PM
    • Loading...
    • JohnnySun
    • Joined on 11-21-2007, 1:39 PM
    • Posts 22
    • Points 39
  • Re: Need help with XPath Query

    12-04-2007, 5:35 PM
    • Loading...
    • ljp007
    • Joined on 06-18-2002, 7:40 PM
    • Posts 155
    • Points 507

    Is there a demo or example of the Linq to XML for supporting XML node sorting? Also, are there any special considerations I should be aware of when installing the .Net 3.5 framework - will it impact any of my .Net 2.0 code?

     

    thanks

  • Re: Need help with XPath Query

    12-04-2007, 7:06 PM
    Answer
    • Loading...
    • JohnnySun
    • Joined on 11-21-2007, 1:39 PM
    • Posts 22
    • Points 39

    Note the following site. You can use Order By to re-arrange your xml file.

    http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

     Code:

    XElement xml = new XElement("contacts",
    from c in db.Contacts
    orderby c.ContactId
    select new XElement("contact",
    new XAttribute("contactId", c.ContactId),
    new XElement("firstName", c.FirstName),
    new XElement("lastName", c.LastName))
    );


    Output:
    <contacts>
    <contact contactId="2">
    <firstName>Barney</firstName>
    <lastName>Gottshall</lastName>
    </contact>
    <contact contactId="3">
    <firstName>Armando</firstName>
    <lastName>Valdes</lastName>
    </contact>
    <contact contactId="4">
    <firstName>Adam</firstName>
    <lastName>Gauwain</lastName>
    </contact>
    ...
    </contacts>

     

  • Re: Need help with XPath Query

    12-04-2007, 7:53 PM
    Answer
    • Loading...
    • ljp007
    • Joined on 06-18-2002, 7:40 PM
    • Posts 155
    • Points 507

    Thanks. 2 more questions. 1) I need to install .Net 3.5 Framework to run Linq queries correct? If so, will .Net Framework have any adverse effect on my apps currently running .Net 2.0? 2) I also want to perform a Top 5 clause to return only a small portion of the dataset. Do you know if I would simply stick the TOP 5 Clause in the statment:

     new XElement("contacts",
                       TOP 5 from c in db.Contacts
                        orderby c.ContactId
    ?

     Thanks

  • Re: Need help with XPath Query

    12-06-2007, 1:44 PM
    • Loading...
    • ljp007
    • Joined on 06-18-2002, 7:40 PM
    • Posts 155
    • Points 507

    Even using Xpath 2.0 I dont see any documentation on how an Xpath query with an order by clause is possible. Is this Xpath 2.0 query possible?

     

    //Collection/Content[(Html/root/DocInfo/Type = 'Case Study') ORDER BY PUBLISHDATE]

  • Re: Need help with XPath Query

    12-06-2007, 10:14 PM
    Answer

    Hi ljp007,

    Another way is to use XPathExpression to sort your data. I think it is easier. You can loop through the nodes set to retrieve top 2 or 5 nodes.

    Have a look at my sample.

    <catalog>
      <faq id="12" type="3" name="bla">12</faq>
      <faq id="10" type="3" name="bla">10</faq>
      <faq id="11" type="1" name="bla">11</faq>
      <faq id="13" type="3" name="bla">13</faq>
    </catalog>

                XPathDocument doc = new XPathDocument(Server.MapPath("doc.xml"));
                XPathNavigator nav = doc.CreateNavigator();
                XPathExpression exp = nav.Compile("/catalog/faq");
                exp.AddSort("@id", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
                XPathNodeIterator iter = nav.Select(exp);
                while (iter.MoveNext())
                {
                    string res = iter.Current.Value;
                }

     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Page 1 of 1 (8 items)