Solved

Simple DOM Question plus XML Sort

Posted on 2004-04-26
5
272 Views
Last Modified: 2012-06-22
We're fairly new to XML so any help appreciated (we are using online help and docs etc but have a fairly sharp timeline...)

We have some XML:

<Press_Components>
   <Component ID="1">
      <Content>
         <title>This is the Title of the News Article A</title>
         <ReleaseDate>
            <day>15</day>
            <month>04</month>
            <year>2004</year>
            <time>
               <hour>08</hour>
               <minute>00</minute>
               <zone>UK</zone>
            </time>
         </ReleaseDate>
         <Synopsis>This is the synopsis 1</Synopsis>
      </Content>

      <Categories>
         <Category Title="BIA">Agility</Category>
      </Categories>
   </Component>

   <Component ID="2">
      <Content>
         <title>This is the Title of the News Article B</title>>
         <ReleaseDate>
            <day>15</day>
            <month>04</month>
            <year>2004</year>
            <time>
               <hour>08</hour>
               <minute>00</minute>
               <zone>UK</zone>
            </time>
         </ReleaseDate>
         <Synopsis>This is the synopsis 2</Synopsis>
      </Content>

      <Categories>
         <Category Title="BIA">Cost Reduction</Category>

         <Category Title="Solution">Development</Category>

         <Category Title="Industry">Financial Services</Category>
      </Categories>
   </Component>
<Press_Components>

We have some DOM code in ASP.

'Output All events in the XML
Response.Write "<h2>All Events </h2>"
For each oNode in oDom.SelectNodes("//Component")
      Response.Write oNode.GetAttribute("ID") & " - "
      Response.Write oNode.getElementsByTagName("title").item(0).text & " - <br/>"
      Response.Write oNode.getElementsByTagName("Synopsis").item(0).text & " - <br/><br/>"
Next

This works but is this the correct/best way to get these elements.

In terms of a sort, how would we use XSL to sort the components so that they came out in reverse order of ReleaseDate - Year, Month, Day, Hour, Minutes... (hour will be 24 hour clock).

Many thanks

0
Comment
Question by:mfwebteam
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:Yury_Delendik
ID: 10922524
This one looks better:
     Response.Write oNode.SelectSingleNode("title").text & " - <br/>"
     Response.Write oNode.SelectSingleNode("Synopsis").text & " - <br/><br/>"

0
 
LVL 10

Accepted Solution

by:
Yury_Delendik earned 250 total points
ID: 10922596
Sorry:
     Response.Write oNode.SelectSingleNode("Content/title").text & " - <br/>"
     Response.Write oNode.SelectSingleNode("Content/Synopsis").text & " - <br/><br/>"

And XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="comp_order" select="'descending'" /> <!-- for ascending -->

<xsl:template match="/">
<html>
<body>
  <h2>All Events </h2>
  <xsl:apply-templates select="//Component">
      <xsl:sort select="Content/year" order="{$comp_order}" />
      <xsl:sort select="Content/month" order="{$comp_order}" />
      <xsl:sort select="Content/day" order="{$comp_order}" />
      <xsl:sort select="Content/time/hour" order="{$comp_order}" />
      <xsl:sort select="Content/time/minute" order="{$comp_order}" />
  </xsl:apply-templates>
</body>
</html>
</xsl:template>


<xsl:template match="Component">
    <xsl:value-of select="@ID" /> -
    <xsl:value-of select="Content/title" /> - <br/>
    <xsl:value-of select="Content/Synopsis" /> - <br/><br/>
</xsl:template>

</xsl:stylesheet>
0
 

Author Comment

by:mfwebteam
ID: 10929348
Thanks Yury.  That works well.  One final thing.  I've adjusted the XSL slightly but want to put a space between the Date and Time (e.g. 2004-04-15 08:00) - see below. How is that done?


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

<xsl:param name="comp_order" select="'descending'" /> <!-- for ascending -->

<xsl:template match="/">
<html>
<body>
  <h2>All Events </h2>
  <xsl:apply-templates select="//Component">
     <xsl:sort select="Content/ReleaseDate/year" order="{$comp_order}" />
     <xsl:sort select="Content/ReleaseDate/month" order="{$comp_order}" />
     <xsl:sort select="Content/ReleaseDate/day" order="{$comp_order}" />
     <xsl:sort select="Content/ReleaseDate/time/hour" order="{$comp_order}" />
     <xsl:sort select="Content/ReleaseDate/time/minute" order="{$comp_order}" />
  </xsl:apply-templates>
</body>
</html>
</xsl:template>


<xsl:template match="Component">
    <strong>ID: </strong><xsl:value-of select="@ID" /><br />
    <strong>Date: </strong><xsl:value-of select="Content/ReleaseDate/year" />-<xsl:value-of select="Content/ReleaseDate/month" />-<xsl:value-of select="Content/ReleaseDate/day" />  <xsl:value-of select="Content/ReleaseDate/time/hour" />:<xsl:value-of select="Content/ReleaseDate/time/minute" /> <br />
    <strong>Title: </strong><xsl:value-of select="Content/title" /><br/>
    <strong>Synopsis: </strong><xsl:value-of select="Content/Synopsis" /> - <br/><br/>
</xsl:template>

</xsl:stylesheet>
0
 
LVL 15

Assisted Solution

by:dualsoul
dualsoul earned 20 total points
ID: 10932199
>I've adjusted the XSL slightly but want to put a space between the Date and Time

you can write space within <xsl:text>, see:
....................................................
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="comp_order" select="'descending'" /> <!-- for ascending -->

<xsl:template match="/">
<html>
<body>
  <h2>All Events </h2>
  <xsl:apply-templates select="//Component">
     <xsl:sort select="Content/ReleaseDate/year" order="{$comp_order}" />
     <xsl:sort select="Content/ReleaseDate/month" order="{$comp_order}" />
     <xsl:sort select="Content/ReleaseDate/day" order="{$comp_order}" />
     <xsl:sort select="Content/ReleaseDate/time/hour" order="{$comp_order}" />
     <xsl:sort select="Content/ReleaseDate/time/minute" order="{$comp_order}" />
  </xsl:apply-templates>
</body>
</html>
</xsl:template>


<xsl:template match="Component">
    <strong>ID: </strong><xsl:value-of select="@ID" /><br />
    <strong>Date: </strong><xsl:value-of select="Content/ReleaseDate/year" />-<xsl:value-of select="Content/ReleaseDate/month" />-<xsl:value-of select="Content/ReleaseDate/day" /><xsl:text> </xsl:text><xsl:value-of select="Content/ReleaseDate/time/hour" />:<xsl:value-of select="Content/ReleaseDate/time/minute" /> <br />
    <strong>Title: </strong><xsl:value-of select="Content/title" /><br/>
    <strong>Synopsis: </strong><xsl:value-of select="Content/Synopsis" /> - <br/><br/>
</xsl:template>

</xsl:stylesheet>
....................................................
0
 

Author Comment

by:mfwebteam
ID: 10939716
Thanks Yury and dualsoul.  

Dualsole - accepted Yury's as the main answer but given you 20 points for the simpler question.

Thanks for your help
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now