469,331 Members | 4,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Please! Help me with this problem (Its urgent for my project)

Hi,

I'am developing a project for training management in a company where
the department trains the internal employees. I have been given the job
to display the reports. Where I got stuck with the following problem.
Consider, the following xml file (Employee.xml)
<?xml version="1.0"?>
<employees>
<employee>
<name>Ravi</name>
<desc>My favourite <a
href='http://www.mail.yahoo.com'>page</a></desc>
</employee>
</employees>
Consider the XSL file for this (Employee.xsl)
<?xml version = '1.0' encoding = 'utf-8'?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
Name :<xsl:value-of
select='/employees/employee/name'/><br/>
<xsl:value-of
select='/employees/employee/desc'/>
</xsl:if>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Which gives following output:
Name :Ravi
My favourite <a href='http://www.mail.yahoo.com'>page</a>
Now my requirement is to display "page" as a hyper link instead of
displaying
<a href='http://www.mail.yahoo.com'>page</a>

Feb 10 '06 #1
9 1481
> Now my requirement is to display "page" as a hyper link instead of
displaying
<a href='http://www.mail.yahoo.com'>page</a>


Uhm... If you're displaying that in an HTML browser, the <a> *is* a
hyperlink and should be doing the right thing.

If you aren't... then what tool are you using to display it? You need to
produce whatever that tool recognizes as a hyperlink.
Feb 10 '06 #2
Actually, I think you've mis-described the problem.

xsl:value-of normally copies only the text content, not the markup, so I
presume the actual bug is that you want

My favourite <a href='http://www.mail.yahoo.com'>page</a>

but are getting

My favourite page

If so, the fix is straightforward: you want to copy the contents of the
<desc> element as they stand. One way to do so would be to replace
<xsl:value-of select='/employees/employee/desc'/>
with
<xsl:copy-of select='/employees/employee/desc/*'/>

Note that your example won't work once you have more than one employee.
You'll need to divide this into stages: find all the employees, then
process each employee's content. I'd really suggest you take the time to
look at some tutorial examples, which will give you a better
introduction to XSLT. I've been citing IBM's DeveloperWorks site and
the XSLT FAQ collection pretty often for the past week; there are plenty
of others but those (and the actual W3C Recommendation spec) are where I
tend to start.
Feb 10 '06 #3
On 10 Feb 2006 12:02:32 -0800, "Fazal" <sy************@yahoo.com> wrote:
Now my requirement is to display "page" as a hyper link instead of
displaying
<a href='http://www.mail.yahoo.com'>page</a>
Your problem appears to boil down to "How do I generate attibutes?"

try this fragment:

<a href='{a/@href}'<xsl:value-of select='desc' /></a>


or else look up the <xsl:attribute> element
Feb 11 '06 #4
Thanks for all your suggestions,

Let me describe my requirement further more.
Actually I'am working on an XML file which is an output from some
internal framework which I cannot modify (so a/@href cannot be done).
The XML file has <desc> tag which contains the employee description as
follows:
My page is &lt;a href="www.mail.yahoo.com"&gt; Page &lt;/a&gt;
If you notice < symbol is replaced with &lt; and > by &gt;

Now I have to generate an XSL code which can intercept this &lt; as a
tag and display the "Page" as hyperlink. Rather than displaying <a
href='http://www.mail.yahoo.com'>Page</a>

Please! reply as soon as possible.

Feb 11 '06 #5
Sorry please ignore the previous post.
Thanks for all your suggestions,

Let me describe my requirement further more.
Actually I'am working on an XML file which is an output from some
internal framework which I cannot modify (so a/@href cannot be done).
The XML file has <desc> tag which contains the employee description as
follows:
My page is & lt ;a href="www.mail.yahoo.com"& gt ; Page & lt ;/a& gt ;
If you notice < symbol is replaced with & lt ; and > by & gt ; (Where &
lt ; and & gt ; are HTML entities for < and >)

Now I have to generate an XSL code which can intercept this < as a
tag and display the "Page" as hyperlink. Rather than displaying <a
href='http://www.mail.yahoo.com'>Page</a>
Please! reply as soon as possible.

Feb 11 '06 #6
See the comments recently about trying to change embedded URIs into
links. Parsing the contents of a string is not where XSLT is strongest.
You can do it, but (a) first you have to come up with a complete
definition of exactly what you want to do (just <a> tags, or all
markup-in-text?), and then you have to code it yourself out of basic
string operations and (probably) recursion in lieu of iteration.

One you've found it and extracted the data, creating the new element is
trivial, of course.

You may be better off coding this manually. Run the document through a
parser, run the content which you need to re-parse through the parser
again, and use the latter to fix the former.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Feb 11 '06 #7
Thanks Joe you have caught my problem.

Can you please help me with the code where string operations are used.
I cannot use parser as I have to develop XSL file for this.

Feb 12 '06 #8
Fazal wrote:
Can you please help me with the code where string operations are used.
I cannot use parser as I have to develop XSL file for this.


Sorry, that's a much bigger hassle than I'll tackle for free, and I
suspect you can't afford me.

The best I can do is suggest that you study some of the examples in the
XSL FAQ pages (http://www.dpawson.co.uk/xsl/xslfaq.html). The "general
string replace" and "strings" pages will give you some of the basic
techniques. Beyond that, reread the XSLT and XPath specs to make sure
you know which string functions are available.

After that, it's all coding. Use those approaches to write logic that
scans the string content for the pseudo-markup you're interested in,
extract the data from that, and build new element/attribute/text nodes
as approrpriate.

Frankly, if I was doing it, I'd be inclined to write an XSLT extension
function that called an XML parser and make that do the work for me,
unless portability was needed. I'd be even more inclined to go back to
the customer and say "You're fixing the wrong tool. Generate proper XML
at the source."

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Feb 12 '06 #9
If you were working in XSLT 2.0, there's actually an HTML parser written
in XSLT -- http://www.dcarlisle.demon.co.uk/htmlparse.xsl -- but I
believe that requires features not available in XSLT 1.0. You might want
to look at it for ideas, though, if you're really determined to do
something this ugly.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Feb 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by m3ckon | last post: by
1 post views Thread by Ex-Em-El | last post: by
3 posts views Thread by Mark Broadbent | last post: by
31 posts views Thread by Simply_Red | last post: by
4 posts views Thread by Jui.Acharya | last post: by
1 post views Thread by =?Utf-8?B?Sm9obiBXYWxrZXI=?= | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.