XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 13th, 2004, 08:08 AM
verolaet verolaet is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: France/Paris
Posts: 30 verolaet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 30 sec
Reputation Power: 6
Exclamation Help on Xpath expression

Hello

I have a XML file which looks like this

<root>
<flds id="flds1">
<fld name="name1" value="value1" />
<fld name="name2" value="value2" />
<fld name="name3" value="valuename31" />
<fld name="name4" value="valuename41" />
</flds>
<flds id="flds2">
<fld name="name1" value="value1" />
<fld name="name2" value="value2" />
<fld name="name3" value="valuename32" />
<fld name="name4" value="valuename42" />
</flds>
<flds id="flds3">
<fld name="name1" value="valuename13" />
<fld name="name2" value="valuename23" />
<fld name="name3" value="valuename33" />
<fld name="name4" value="valuename43" />
</flds>
<flds id="flds4">
<fld name="name1" value="valuename14" />
<fld name="name2" value="valuename24" />
<fld name="name3" value="valuename34" />
<fld name="name4" value="valuename44" />
</flds>
</root>

I want to get in a node set,
ALL the <flds> containing
a <fld> with attribute name1 with value = 'value1' AND
a <fld> with attribute name2 with value = 'value2'

How can I do that?

Thanks a lot for your soon quick answer!
This feature is really blocking my dev!

Vero

Reply With Quote
  #2  
Old April 13th, 2004, 09:21 AM
fpmurphy fpmurphy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: USA
Posts: 282 fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 2 h 30 m 42 sec
Reputation Power: 6
Here is an example of a quick and dirty way
of doing it. There are more elegant solutions!

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
 
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

  <xsl:template match="/">
      <xsl:variable name="tmp" select="/root/flds[./fld[@name='name1' and @value='value1']]" />
      <xsl:apply-templates select="$tmp[./fld[@name='name2' and @value='value2']]" />
  </xsl:template>

  <xsl:template match="/root/flds">
     Node Set: 
     <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

Reply With Quote
  #3  
Old April 13th, 2004, 09:32 AM
khwang's Avatar
khwang khwang is offline
Über nübe
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Babylon 4
Posts: 240 khwang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 27 sec
Reputation Power: 6
Your xml doc has no values, but if it did, try it with this XSL:

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

  <xsl:output method="html" 
          doctype-public="-//W3C//DTD XHTML 1.1//EN" 
          doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

<xsl:template match="root"><!--root-->
<html>
<body>
	<xsl:for-each select = "(flds/fld[@name='name1' and @value='value1'])">
	<xsl:value-of select="." />
	</xsl:for-each>	

</body>
</html>
</xsl:template>
</xsl:stylesheet>
__________________
Hello, old friend...

Reply With Quote
  #4  
Old April 13th, 2004, 09:57 AM
verolaet verolaet is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: France/Paris
Posts: 30 verolaet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 30 sec
Reputation Power: 6
Thanks fpmurphy for your quick answer.

First I had forgotten the "./" oups...
It seems strange that we can't directly take the node set like

<xsl:variable name="tmp" select="/root/flds[./fld[@name='name1' and value='value1']]" />
<xsl:value-of select="count ($tmp[./fld[@name='name2' and @value='value2']] )" />

(I need to retrive the nb of elements)

Do you know why we can't do it directly?

thx
Vero

Reply With Quote
  #5  
Old April 13th, 2004, 10:26 AM
verolaet verolaet is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: France/Paris
Posts: 30 verolaet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 30 sec
Reputation Power: 6
Ok, it works like this! I did a character case mistake! so the global answer is:

<xsl:variable name="nodes" select="/root/flds[./fld[@name = 'name1' and @Value = 'value1']]" />
<xsl:value-of select="count($nodes/fld[@name='name2' and @value='value2']) "/>

Thanks all

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Help on Xpath expression


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT