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 January 27th, 2004, 09:58 AM
el1179 el1179 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 18 el1179 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 5 sec
Reputation Power: 0
more xpath Help needed

I am currently searching various xpath tutorials in order to try and figure this out, but wanted to post it here as well to see if someone could help me out.

Say I have the following xml document:


<AAA>
<BBB elementID="1234">
<CCC string= "asdf"/>
</BBB>
<GGG elementID="5678">
<DDD/>
<EEE>
<FFF string="hjkl"/>
</EEE>
</GGG>
<HHH>
<III/>
</HHH>
</AAA>


What I am trying to do is...anywhere there is a "string" select it AND the preceding "elementID" that goes with it. A string will always have an element ID preceding it.

After selecting I willprint them on a line in this fashion. elementID1value, stringvalue1
elementID2value, stringvalue2
etc.

The trick is that the "string" can be embedded one level or many levels. Any suggestions?

Reply With Quote
  #2  
Old January 27th, 2004, 03:39 PM
MattSidesinger's Avatar
MattSidesinger MattSidesinger is offline
Java PHP Oracle Developer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: C-Bus OH-IO
Posts: 204 MattSidesinger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 26 sec
Reputation Power: 5
Send a message via AIM to MattSidesinger
Solution

Does anyone have another solution?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:call-template name="printPairs">
<xsl:with-param name="startNode" select="/"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="printPairs">
<xsl:param name="startNode"/>
<xsl:param name="mostRecentElementId"/>

<xsl:if test="@string">
<xsl:value-of select="$mostRecentElementId"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="$startNode/@string"/>
<xsl:text>
</xsl:text>
</xsl:if>

<xsl:for-each select="$startNode/child::node()">
<xsl:choose>
<xsl:when test="@elementID">
<xsl:call-template name="printPairs">
<xsl:with-param name="startNode" select="."/>
<xsl:with-param name="mostRecentElementId" select="@elementID"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="printPairs">
<xsl:with-param name="startNode" select="."/>
<xsl:with-param name="mostRecentElementId" select="$mostRecentElementId"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

Reply With Quote
  #3  
Old January 28th, 2004, 10:37 AM
fpmurphy fpmurphy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: USA
Posts: 259 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: 23 h 8 m 38 sec
Reputation Power: 6
> Does anyone have another solution?

I think your solution is as good as it gets

- F

Reply With Quote
  #4  
Old January 28th, 2004, 02:30 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Well, I don't know that this is any better, but it should be shorter at least.

"//.[boolean(@string)]" will give you all the nodes with a string attribute.

"./ancestor::node()[boolean(@elementID)]" will give you all the parent nodes of the current node that have elementID attributes. This works great if you don't have nested elementID nodes, otherwise you have to loop through the node-set to get the last() node.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > more xpath Help needed


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 4 hosted by Hostway