|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Selecting tags + values from context node only |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|