Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xsl to retrieve text in table elements in xml

Reply
Thread Tools

xsl to retrieve text in table elements in xml

 
 
Figo 775
Guest
Posts: n/a
 
      11-16-2004


Hi,
I have an xml document which is as below:
<?xml version="1.0" encoding="UTF-8"?>
<fragment name="htmlPart">
<value>&lt;table&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Feature&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b
&gt;Benefit&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
sample text runs here too&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
sample text runs here too&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Now I am here &lt;/td&gt;&lt;td&gt;Now what am I
going to do here&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some stuff lies here too&lt;/td&gt;&lt;td&gt;Please
give suggestions&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
</value>
</fragment>

3 "fragment name ="htmlpart" elements are present in this document..

For each fragment name ="htmlpart", the xsl I need should create an
element "<fragment name="tr"> and map the tr values from the text above
in its "value" element.
The same needs to be done for "td".
Please provide an answer to this.

Thanks,
figo


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-16-2004


Figo 775 wrote:


> I have an xml document which is as below:
> <?xml version="1.0" encoding="UTF-8"?>
> <fragment name="htmlPart">
> <value>&lt;table&gt;
> &lt;tr&gt;&lt;td&gt;&lt;b&gt;Feature&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b
> &gt;Benefit&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
> &lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
> sample text runs here too&lt;/td&gt;&lt;/tr&gt;
> &lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
> sample text runs here too&lt;/td&gt;&lt;/tr&gt;
> &lt;tr&gt;&lt;td&gt;Now I am here &lt;/td&gt;&lt;td&gt;Now what am I
> going to do here&lt;/td&gt;&lt;/tr&gt;
> &lt;tr&gt;&lt;td&gt;some stuff lies here too&lt;/td&gt;&lt;td&gt;Please
> give suggestions&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
> </value>
> </fragment>
>
> 3 "fragment name ="htmlpart" elements are present in this document..
>
> For each fragment name ="htmlpart", the xsl I need should create an
> element "<fragment name="tr"> and map the tr values from the text above
> in its "value" element.
> The same needs to be done for "td".
> Please provide an answer to this.


I am afraid as your XML input doesn't contain <tr> elements or <td>
elements but just text with such tags there is no easy way to process
the input with XSLT/XPath.
All you could do is use the XPath text processing but there are
languages which have more power for text processing than XSLT/XPath 1.0
have.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      11-16-2004
> I have an xml document which is as below:
> <?xml version="1.0" encoding="UTF-8"?>
> <fragment name="htmlPart">
> <value>&lt;table&gt;
> &lt;tr&gt;&lt;td&gt;&lt;b&gt;Feature&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b
> &gt;Benefit&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
> &lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
> sample text runs here too&lt;/td&gt;&lt;/tr&gt;
> &lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
> sample text runs here too&lt;/td&gt;&lt;/tr&gt;
> &lt;tr&gt;&lt;td&gt;Now I am here &lt;/td&gt;&lt;td&gt;Now what am I
> going to do here&lt;/td&gt;&lt;/tr&gt;
> &lt;tr&gt;&lt;td&gt;some stuff lies here too&lt;/td&gt;&lt;td&gt;Please
> give suggestions&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
> </value>
> </fragment>
>


The html encoded as text looks like valid XHMLT, so you should include the html as elements rather than raw text. So the first step would be to unescape the &lt; and &gt; entities. (It can even be done with XSLT)

Once you have this XML:
<fragment name="htmlPart">
<value><table> <tr><td><b>Feature</b></td><td><b >Benefit</b></td></tr> <tr><td>some sample text runs here</td><td>some sample text runs here too</td></tr> <tr><td>some sample text runs here</td><td>some sample text runs here too</td></tr> <tr><td>Now I am here </td><td>Now what am I going to do here</td></tr> <tr><td>some stuff lies here too</td><td>Please give suggestions</td></tr></table>
</value>
</fragment>

the job is easily done with XSLT like this:
<xsl:template match="fragment[@name='htmlPart']">
<value>
<xsl:apply-templates select="value/*"/>
</value>
</xsl:template>

<xsl:template match="tr|td">
<fragment name="{local-name()}">
<value>
<xsl:apply-templates/>
</value>
</fragment>
</xsl:template>


I'm not sure though, if this is really what you mean with "mapping the tr values from the text its "value" element". Please explain a bit clearer what is your goal.

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
 
Reply With Quote
 
Figo 775
Guest
Posts: n/a
 
      11-16-2004


Hi Joris,

I think I will be more clearer now though this is a small portion of a
bigger doc. Please let me know if I am not.

<?xml version="1.0" encoding="UTF-8"?>
<fragment name="htmlPart">
<value>&lt;table&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Feature&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b
&gt;Benefit&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
sample text runs here too&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
sample text runs here too&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Now I am here &lt;/td&gt;&lt;td&gt;Now what am I
going to do here&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some stuff lies here too&lt;/td&gt;&lt;td&gt;Please
give suggestions&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
</value>
</fragment>

3 "fragment name ="htmlpart" elements are present(similar to the above
format.

My XSL should convert the "htmlpart" element into three elements
<fragment name="tr">
<value>
<fragment name="td">
<value>
<fragment name="tablecell">
<value>
This should contain text between the first '&lt;tr&gt;&lt;tr&gt;'
'&lt;/td&gt;&lt;/tr&gt;'
</value>
</fragment>
....
This structure should be repeated for each content text between
'&lt;tr&gt;&lt;tr&gt;' '&lt;/td&gt;&lt;/tr&gt;' .
So I guess I need to use substring recursively to search for the above
patterns and map content. Hope this gives a better picture. Any help
would be greatly appreciated.
Thanks,
figo


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

 
Reply With Quote
 
Figo 775
Guest
Posts: n/a
 
      11-16-2004
Hi khp,

I think I will be more clearer now though this is a small portion of a
bigger doc.

<?xml version="1.0" encoding="UTF-8"?>
<fragment name="htmlPart">
<value>&lt;table&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Feature&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b
&gt;Benefit&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
sample text runs here too&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some sample text runs here&lt;/td&gt;&lt;td&gt;some
sample text runs here too&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Now I am here &lt;/td&gt;&lt;td&gt;Now what am I
going to do here&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;some stuff lies here too&lt;/td&gt;&lt;td&gt;Please
give suggestions&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
</value>
</fragment>

3 "fragment name ="htmlpart" elements are present(similar to the above
format.

My XSL should convert the "htmlpart" element into three elements
<fragment name="tr">
<value>
<fragment name="td">
<value>
<fragment name="tablecell">
<value>
This should contain text between the first '&lt;tr&gt;&lt;tr&gt;'
'&lt;/td&gt;&lt;/tr&gt;'
</value>
</fragment>
....
This structure should be repeated for each content text between
'&lt;tr&gt;&lt;tr&gt;' '&lt;/td&gt;&lt;/tr&gt;' .
Hope this gives a better picture. Any help would be greatly appreciated.
Thanks,
figo




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retrieve XML CDATA text contents by org.xml.sax.ext.DefaultHandler2? RC Java 6 05-04-2009 11:33 PM
How to retrieve XML CDATA text contents by org.xml.sax.ext.DefaultHandler2? RC XML 6 05-04-2009 11:33 PM
xsl to group elements? [xsl newbie] Rob Smegma XML 1 09-26-2005 10:59 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
Using XSL to delete fixed text in arbitrary elements Fred Bartlett XML 1 02-17-2004 09:24 PM



Advertisments