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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old January 12th, 2003, 12:18 PM
Ultrahonk Ultrahonk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 1 Ultrahonk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Selecting tags + values from context node only

I have an xml document that's structured like this:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<website>

 <content name="fooName" link="fooLink.html">

  <text heading="fooHeading1">
   <p>foo1</p>
  </text>
  
  <text heading="fooHeading2">
   <p>foo2</p>
  </text>

 </content>

 <content name="barName" link="barLink.html">
 
  <text heading="barHeading1">
   <p>bar1</p>
  </text>
  
 </content>


What I want to do now is to select exactly one content-node and extract a) the name-attribut b) the link-attribut and c) all text-nodes + all their children which I want to display without further processing. I achieved a) and b) with relative ease by using the following stylesheet:

Code:
<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:template match="/">

    <div class="content">

     <xsl:apply-templates/>

    </div>

</xsl:template>

<xsl:template match="//website/content[1]">

<xsl:for-each select="text">
 
     <h2>
      <xsl:value-of select="@heading"/>
     </h2>

      <xsl:value-of select="self::*" />
    
    </xsl:for-each>
    
</xsl:template>

</xsl:stylesheet>


b) is not in the example the XPath query works nevertheless. c) however is a difficult nut for me to crack - whatever I try I always lose the children (the tags, not the content) of the text-node and (what's much worse) I always get also the text-nodes which are not children of the current content-node. I tried lot's of XPath expressions but none seemed to achive this (simple?) goal.

Applying the above mentioned stylesheet (using php-sablotron) results in something like this:

Code:
<h2>fooHeading1</h2>
   foo1
  <h2>fooHeading2</h2>
   foo2
  

 
 
  
   bar1


Any suggestions?

Reply With Quote
  #2  
Old January 13th, 2003, 04:48 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
My experience has been that if you want to keep your xml formatted that way, you're going to need to define each element in the xsl. Since your <text> nodes contain child nodes, the xml parser reads these nodes as valid xml, and has no clue they're supposed to be html. So, your xsl needs to be something like:
Code:
<xsl:template match="text">
  <h1>
    <xsl:value-of select="@heading" />
  </h1>
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="p">
  <p>
    <xsl:apply-templates />
  </p>
</xsl:template>

That is, you need to define a transform for each node that may appear in your text nodes. This is both good and bad. Good side is it can force you to use only strict elements inside your nodes, DTDs can also be useful for enfocing the structure. Bad though, if your <text> nodes will contain a smattering of any html elements...in that case, you'd need to basically define the whole html dtd for your xsl transform. Tedious.

If you're not going to have well structured <text> nodes, consider using a CDATA node to contain the text in. This will prevent the parser from reading each p, b, i, etc, element as xml, and allow you to just get the raw value of the CDATA node and dump it through the transform.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Selecting tags + values from context node only


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