xsl to retrieve text in table elements in xml

Discussion in 'XML' started by Figo 775, Nov 16, 2004.

  1. Figo 775

    Figo 775 Guest

    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!
     
    Figo 775, Nov 16, 2004
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Nov 16, 2004
    #2
    1. Advertising

  3. Figo 775

    Joris Gillis Guest

    > 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-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Nov 16, 2004
    #3
  4. Figo 775

    Figo 775 Guest

    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!
     
    Figo 775, Nov 16, 2004
    #4
  5. Figo 775

    Figo 775 Guest

    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!
     
    Figo 775, Nov 16, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred Bartlett
    Replies:
    1
    Views:
    744
    Dimitre Novatchev [MVP XML]
    Feb 17, 2004
  2. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  3. Rob Smegma
    Replies:
    1
    Views:
    1,817
    shaun
    Sep 26, 2005
  4. RC
    Replies:
    6
    Views:
    607
  5. RC
    Replies:
    6
    Views:
    3,739
Loading...

Share This Page