Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nodes from latest date attribute 1

Status
Not open for further replies.

stinkybee

Programmer
May 15, 2001
218
GB
Is there a way to get a node set from an xml file based on a date attribute. For example, I would like to get the child nodes of an element with a date attribute that has the latest date in it.

example xml

Code:
<data>
  <element date="01/01/06">
      ...child nodes of element
  </element>
  <element date="02/01/06">
      ...child nodes of element
  </element>
</data>

so using this exmaple I would like to retrieve the child elements of the element with a date attribute of "02/01/06", which is the latest date. I have been looking at XPath which looks like it might do the job but haven't found a specific solution to this problem.
 
Do you have any control over the format of the date attribute value? if so, you could use xsl:sort on that attribute. (But you didn't mention what technology you are using to manipulate your XML document...)

Tom Morrison
 
I am using asp, the date format doesn't matter to me really. I'm just using that as a possible example, I would be just as happy with an answer using a number instead of the date.

for example, getting the child nodes of the "element" with the highest id.

<data>
<element id="1">
...child nodes of element
</element>
<element id="2">
...child nodes of element
</element>
</data>
 
This will work for a moderate size document. For a larger document, you should use a recursive solution. First, a slightly more interesting document:
Code:
<data>
  <element id="4">
      <someNode>This came from 4</someNode>
  </element>
  <element id="1">
      <someNode>This came from 1</someNode>
  </element>
  <element id="3">
      <someNode>This came from 3</someNode>
  </element>
  <element id="5">
      <someNode>This came from 5</someNode>
  </element>
  <element id="2">
      <someNode>This came from 2</someNode>
  </element>
</data>

Using this stylesheet:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:template match="/">
	<valueFound><xsl:value-of 
	             select="/data/element[not(../element/@id &gt; @id)]/someNode" />
	</valueFound>
</xsl:template>
</xsl:stylesheet>

This result produces:
Code:
<valueFound>This came from 5</valueFound>

Since you are using ASP (about which I know very little) I would guess that you would use the transformNode method.

Tom Morrison
 
Here is a recursive solution that will probably work better for larger documents:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:template match="/">
	<valueFound><xsl:apply-templates select="/data/element[1]" mode="find-maximum-id" />
	</valueFound>
</xsl:template>
<xsl:template match="element" mode="find-maximum-id">
   <xsl:variable name="next"
                 select="following-sibling::element[@id &gt; current()/@id]" />
   <xsl:choose>
      <xsl:when test="$next">
         <xsl:apply-templates select="$next" mode="find-maximum-id" />
      </xsl:when>
      <xsl:otherwise>
         <xsl:value-of select="someNode" />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>
</xsl:stylesheet>

Tom Morrison
 
Thanks for these, they work a treat. Just in case anyone has a similar problem with ASP here is the code I used without using XSL.

Code:
set objLst = objXML.selectNodes("/data/element[not(../element/@id > @id)]/*")

This selects all child nodes of the "element" with the highest "id" attribute.
 
whilst we are on the subject, is there a way to extend this so that it selects the child nodes of an element based on the highest value of an attribute where there are two attributes.

example xml

Code:
<data>
  <element id="4" id2="">
      <someNode>This came from 4</someNode>
  </element>
  <element id="1" id2="">
      <someNode>This came from 1</someNode>
  </element>
  <element id="3" id2="6">
      <someNode>This came from 3</someNode>
  </element>
  <element id="5" id2="">
      <someNode>This came from 5</someNode>
  </element>
  <element id="2" id2="">
      <someNode>This came from 2</someNode>
  </element>
</data>

in this case I would like to pull out the values from the element with the highest "id" or the highest "id2", whichever is greater.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top