473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Help with Querying XML Document

VJ
Hello All,

The xml document below describes the contents of a folder in a
Document Managment System. I need to retrieve the DocId for the most
recently added (<Add_Date>) spreadsheet file(<name> ends with ".XLS").
As a newbie to XML, XSL and XPATH, I wanted to bounce some ideas off
the more experienced folks in this group.

<?xml version="1.0" encoding="utf-8" ?>
<FolderDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<Docs>
<DocumentDetails>
<Name>EIU.xls</Name>
<Version>1</Version>
<Add_Date>10/8/2003 3:41:51 PM</Add_Date>
<DocType>E</DocType>
<DocId>15607779</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/17/2003 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
</Docs>
</FolderDetails>
My first thought was to transform this XML into another XML which is
sorted on Add_date (descending order) and then pick the very first
occurance of docId. But there does not seem to be a direct way to
sort on a date value.

I know this is an awfully inadequate explanation of what I am
attempting to do. But I am asking for any ideas or sample code to
quickly and efficiently accomplish this. I am under a tremendous
amount of pressure so any help, ideas, code snippets.. gratefully
accepted...

Thanks in advance,
Jay
Jul 20 '05 #1
4 1854
> The xml document below describes the contents of a folder in a
Document Managment System. I need to retrieve the DocId for the most
recently added (<Add_Date>) spreadsheet file(<name> ends with ".XLS").
This transformation:

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

<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="Docs">
<xsl:for-each
select="DocumentDetails[substring(Name,
string-length(Name) - 3
)
=
'.xls'
]">
<xsl:sort
select="substring-before(substring-after(substring-after(Add_Date, '/'),
'/'), ' ')"
data-type="number"/>

<xsl:sort select="substring-before(Add_Date, '/')"
data-type="number"/>

<xsl:sort select="substring-before(substring-after(Add_Date, '/'),
'/')"
data-type="number"/>

<xsl:sort select="substring(Add_Date, string-length(Add_Date) - 1)"
/>

<xsl:sort
select="translate(substring-before(substring-after(Add_Date, ' '), ' '),
':', '')"
data-type="number"/>

<xsl:if test="position() = last()">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

when applied on this source.xml:

<FolderDetails>
<Docs>
<DocumentDetails>
<Name>EIU.xls</Name>
<Version>1</Version>
<Add_Date>10/8/2003 3:41:51 PM</Add_Date>
<DocType>E</DocType>
<DocId>15607779</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/18/2001 8:22:43 PM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/18/2001 8:22:44 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/18/2001 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/17/2001 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>9/17/2001 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
</Docs>
</FolderDetails>

will correctly sort it by Add_Date (sorting correctly by year, month, date,
AM/PM and time) and produce the DocumentDetails element with the latest
value of Add_Date. In this particular case the result is:

<DocumentDetails>
<Name>EIU.xls</Name>
<Version>1</Version>
<Add_Date>10/8/2003 3:41:51 PM</Add_Date>
<DocType>E</DocType>
<DocId>15607779</DocId>
</DocumentDetails>

If one does not want to write such code, they may use the str-split-to-words
template from FXSL.
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"VJ" <vr****@lucent.com> wrote in message
news:b9**************************@posting.google.c om... Hello All,

The xml document below describes the contents of a folder in a
Document Managment System. I need to retrieve the DocId for the most
recently added (<Add_Date>) spreadsheet file(<name> ends with ".XLS").
As a newbie to XML, XSL and XPATH, I wanted to bounce some ideas off
the more experienced folks in this group.

<?xml version="1.0" encoding="utf-8" ?>
<FolderDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<Docs>
<DocumentDetails>
<Name>EIU.xls</Name>
<Version>1</Version>
<Add_Date>10/8/2003 3:41:51 PM</Add_Date>
<DocType>E</DocType>
<DocId>15607779</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/17/2003 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
</Docs>
</FolderDetails>
My first thought was to transform this XML into another XML which is
sorted on Add_date (descending order) and then pick the very first
occurance of docId. But there does not seem to be a direct way to
sort on a date value.

I know this is an awfully inadequate explanation of what I am
attempting to do. But I am asking for any ideas or sample code to
quickly and efficiently accomplish this. I am under a tremendous
amount of pressure so any help, ideas, code snippets.. gratefully
accepted...

Thanks in advance,
Jay

Jul 20 '05 #2
VJ
Hi Dimitre,

Thank you for your response. It works like a charm IF I don't have the
namespace declaration as part of the root node <FolderDetails>.
However since the xml I receive already has the namespace declaration
in it, I can not seem to get this to produce the desired result.
if this :
<FolderDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">

looks like this:

<FolderDetails>

Then your xsl is the answer. But since it isn't any ideas on what
should be tweaked?..
Thank you,
Jay
"Dimitre Novatchev" <dn********@yahoo.com> wrote in message news:<bo*************@ID-152440.news.uni-berlin.de>...
The xml document below describes the contents of a folder in a
Document Managment System. I need to retrieve the DocId for the most
recently added (<Add_Date>) spreadsheet file(<name> ends with ".XLS").


This transformation:

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

<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="Docs">
<xsl:for-each
select="DocumentDetails[substring(Name,
string-length(Name) - 3
)
=
'.xls'
]">
<xsl:sort
select="substring-before(substring-after(substring-after(Add_Date, '/'),
'/'), ' ')"
data-type="number"/>

<xsl:sort select="substring-before(Add_Date, '/')"
data-type="number"/>

<xsl:sort select="substring-before(substring-after(Add_Date, '/'),
'/')"
data-type="number"/>

<xsl:sort select="substring(Add_Date, string-length(Add_Date) - 1)"
/>

<xsl:sort
select="translate(substring-before(substring-after(Add_Date, ' '), ' '),
':', '')"
data-type="number"/>

<xsl:if test="position() = last()">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

when applied on this source.xml:

<FolderDetails>
<Docs>
<DocumentDetails>
<Name>EIU.xls</Name>
<Version>1</Version>
<Add_Date>10/8/2003 3:41:51 PM</Add_Date>
<DocType>E</DocType>
<DocId>15607779</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/18/2001 8:22:43 PM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/18/2001 8:22:44 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/18/2001 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/17/2001 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>9/17/2001 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
</Docs>
</FolderDetails>

will correctly sort it by Add_Date (sorting correctly by year, month, date,
AM/PM and time) and produce the DocumentDetails element with the latest
value of Add_Date. In this particular case the result is:

<DocumentDetails>
<Name>EIU.xls</Name>
<Version>1</Version>
<Add_Date>10/8/2003 3:41:51 PM</Add_Date>
<DocType>E</DocType>
<DocId>15607779</DocId>
</DocumentDetails>

If one does not want to write such code, they may use the str-split-to-words
template from FXSL.
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"VJ" <vr****@lucent.com> wrote in message
news:b9**************************@posting.google.c om...
Hello All,

The xml document below describes the contents of a folder in a
Document Managment System. I need to retrieve the DocId for the most
recently added (<Add_Date>) spreadsheet file(<name> ends with ".XLS").
As a newbie to XML, XSL and XPATH, I wanted to bounce some ideas off
the more experienced folks in this group.

<?xml version="1.0" encoding="utf-8" ?>
<FolderDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<Docs>
<DocumentDetails>
<Name>EIU.xls</Name>
<Version>1</Version>
<Add_Date>10/8/2003 3:41:51 PM</Add_Date>
<DocType>E</DocType>
<DocId>15607779</DocId>
</DocumentDetails>
<DocumentDetails>
<Name>MAKE.xls</Name>
<Version>1</Version>
<Add_Date>10/17/2003 8:22:43 AM</Add_Date>
<DocType>E</DocType>
<DocId>15608338</DocId>
</DocumentDetails>
</Docs>
</FolderDetails>
My first thought was to transform this XML into another XML which is
sorted on Add_date (descending order) and then pick the very first
occurance of docId. But there does not seem to be a direct way to
sort on a date value.

I know this is an awfully inadequate explanation of what I am
attempting to do. But I am asking for any ideas or sample code to
quickly and efficiently accomplish this. I am under a tremendous
amount of pressure so any help, ideas, code snippets.. gratefully
accepted...

Thanks in advance,
Jay

Jul 20 '05 #3
VJ
Dimitre,

Thanks for your response. Your xsl works perfectly IF the
<FolderDetails> root node does not contain namespace declarations.

If the root node looks like this
<FolderDetails>

It works great. However, my xml root looks like this:

<FolderDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">

and so when I do the transform, it lists everything in the xml
document and does not produce the desired result...

Any ideas?.
Thanks again for your reply.
Jay
Jul 20 '05 #4
This is a VFAQ.

Read about the problem and the solution here:

"None of my XPath select statements will work going against an XML file with
a default namespace. Help! "

at:
http://www.topxml.com/people/bosley/defaultns.asp

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"VJ" <vr****@lucent.com> wrote in message
news:b9**************************@posting.google.c om...
Dimitre,

Thanks for your response. Your xsl works perfectly IF the
<FolderDetails> root node does not contain namespace declarations.

If the root node looks like this
<FolderDetails>

It works great. However, my xml root looks like this:

<FolderDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">

and so when I do the transform, it lists everything in the xml
document and does not produce the desired result...

Any ideas?.
Thanks again for your reply.
Jay

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: anthonyberet | last post by:
I work for an organisation that uses a bespoke document imaging system, the database of which is an MS sql server. We have MS Access and already use it for some querying of the database. The...
1
by: Hakan Akkas | last post by:
Hello all, I need to build a search engine wherewith users can query huge Xml Documents (+/- 100 MB) in a user friendly way. The searcher shouldn't be aware of the underlying structure of the...
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
8
by: darnnews | last post by:
I have a form. When a person selects a publication from a listbox, this snippet of code is supposed to look up authors that correspond to that publication and populate the Author List Box, but...
6
by: Thea | last post by:
Hi I am trying to use datatypes defined in xml file to check correctness of input parameter values To define needed datatypes following schema.xml file was created: <?xml version="1.0"?>...
0
by: zhuang | last post by:
Hi, There are some articles online showing how to do this. But my scenario is different, and I could not figure out how to do it at design level. I developed a web application, which will be...
5
by: sql_er | last post by:
Guys, I have an XML file which is 233MB in size. It was created by loading 6 tables from an sql server database into a dataset object and then writing out the contents from this dataset into an...
1
by: Webstorm | last post by:
Hi, I hope someone can help me sort this out a bit, Im completely lost. Here is the page I am working on: http://www.knzbusinessbrokers.com/default.asp I have 3 search critera that I need to...
5
by: wizdom | last post by:
I have a simple message board I created a while back. Recently they've asked me to add threading support for messages. so I created a seperate thread page, which gives me each threaded message...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.