Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2007, 02:46 PM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default can we identify the text in the XSLT?

Hi,

My requirement is to convert the xHTML to XML. I have written the XSLT which is working fine except the following scenario. Some times my input XHTML file may contain some strings without having any nodes or elements. How can i find that using XSLT 1.0? For example, the following is my input file.

Input file
<root>
<p>
<div>
this is the example
</div>

<div>
this is the second example
</div>
This content I want to get
</p>
</root>


I have to find a solution to find the text in the green color? is it possible using XSLT1.0?

Thnaks in advance?

 
Old March 19th, 2007, 02:55 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The normal way of processing narrative text in XSLT is to use a recursive descent using template rules and apply templates. You define a template rule for a particular element that says how that element is to be processed, and then call apply-templates to process its children:

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

Given that design pattern, you can write a template rule that matches your green text (a text node that's immediately within a p element) as

match="p/text()"



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 19th, 2007, 05:59 PM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default

Thanks for immediate response.

If i want to retrieve entire text(ie somany green text may be in the document, which are may be in different level and their parent node may be different) in the document, what can i do?


Thanks in advance.

Raja

 
Old March 19th, 2007, 07:17 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can apply the string() function to any element node to get the concatenation of all the text within that element, at any level. Many operations such as xsl:value-of will do this automatically.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 20th, 2007, 08:09 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default

Hi,

I think i have confused you all. Really sorry for that. Let me explain my requirement clearly. As it is an urgent requirement to fix it, Please help me asap.

I am having XHTML file as input file. I have to convert to XML using the xslt. <root> is the root tag for my input XHTML. If <div> has come in the input file, I have to convert it as <br> and if <p> comes in input file, i have to convert it as <paragraph> . Like this i have to convert all the input tags to output XML formats. Every thing is working fine except taking the text which resides outside the tags.

for Ex consider, the below is as my input file1:

<root>
<p>
<div> test has to be conducted </div>
<div> for checking the patients</div>
whether they are having any contegious disease
</p>
</root>

My output should be

<root>
<paragraph>
<br>test has to be conducted </br>
<br>for checking the patients</br>
whether they are having any contegious disease
</paragraph>
</root>

but if i am using the p/text() the output is coming like

<root>
whether they are having any contegious disease
<paragraph>
<br>test has to be conducted </br>
<br>for checking the patients</br>
</paragraph>
</root>

ie it finds all the strings which are under <p> and put it in first. What I have to do it should be coming in order. For your understanding i will give another example. As it is very urgent for me, please help me



<root>
<div> test has to be conducted </div>
<div> for checking the patients</div>
whether they are having any contegious disease
</root>


the output should be


<root>
<br> test has to be conducted </br>
<br> for checking the patients</br>
whether they are having any contegious disease
</root>


What i am trying to make yo understand is, my input file doesnt have any common structure. It may be in any format.

Thanks in advance



 
Old March 20th, 2007, 08:28 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well it might help if you called things by their correct names, the input isn't XHTML for instance.
Anyway I would do this using the identity transform which produces a copy of the input. You then add extra templates to override behaviour for specific elements:
Code:
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="p">
    <paragraph>
      <xsl:apply-templates select="@*|node()"/>
    </paragraph>
  </xsl:template>

  <xsl:template match="div">
    <br>
      <xsl:apply-templates select="@*|node()"/>
    </br>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old March 20th, 2007, 09:00 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The normal way to handle this is a recursive descent using apply-templates as explained in my first reply. If that's not working for you then you've made some coding mistake and you therefore need to show us your code. (Alternatively, you've ignored my advice, which is a foolish thing to do if you're in a hurry to finish the project)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 22nd, 2007, 08:28 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default

I understand now. Its working fine by using <apply-templates/>

Thanks for everyone.

Regards,
Raja






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using XSLT to generate PDF with Japanese text XSLTUser XSLT 5 December 20th, 2007 09:01 PM
xslt adding text blitzer XSLT 3 May 19th, 2007 10:10 PM
Retrieve hidden text box value through xslt? Hannibal XSLT 1 January 29th, 2007 05:09 AM
Text Replace with XSLT pendyalap XSLT 13 October 1st, 2006 05:51 PM
xslt - need help with the text dev.user06 XSLT 6 July 22nd, 2006 08:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.