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

F

Fazal

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>
 
J

Joe Kesselman

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.
 
J

Joe Kesselman

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.
 
F

Fazal

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.
 
F

Fazal

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.
 
J

Joe Kesselman

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.
 
F

Fazal

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.
 
J

Joe Kesselman

Fazal said:
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."
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top