Jump to content

Need Help With This Html To Xml Transform


Guest Sadd
 Share

Recommended Posts

I am not sure this is the right place for this. But i will try it here. I am very troubled with my XSLT. Trying to transform a text Coupon which has the following html for it. So,1. INPUT is:<html><head></head><body><p>This coupon is for a good guy whose first name is :</p><p></p><p align="center">Sadd</p><p align="center"></p><p align="right"><b>also</b> whose <var>full_name</var> is Sadd Hossain</p><p align="left"></p><p align="left">He is a <font size="3">software </font><font size="4">engineer for</font><font size="5">S&H</font></p></body></html>*2. output needed is:<?xml version="1.0" encoding="UTF-8"?> <POSMESSAGE> <TextMSG> This coupon is for a <emph>good guy</emph> whose first name is : </TextMSG><LF/> <TextMSG align="center"> <SIZE ="3"><emph>SADD</emph></SIZE> </TextMSG> <LF/> <TextMSG align="right"> also whose full_name is Sadd Hossain </TextMSG> <LF/> <TextMSG align="left"> He is a <SIZE ="5"><emph>software engineer</emph></SIZE> for S&H </TextMSG> </POSMESSAGE>*3. XSLT for this<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="2.0"><xsl:template match="body"><POSMESSAGE><xsl:apply-templates/></POSMESSAGE></xsl:template><xsl:template match="p"><TextMSG><xsl:copy-of select="@align"/><xsl:apply-templates/></TextMSG></xsl:template><xsl:template match="b"><emph><xsl:copy-of select="@b"/><xsl:apply-templates/></emph></xsl:template><xsl:template match="font size"><SIZE><xsl:copy-of select="@size"/><xsl:apply-templates/></SIZE></xsl:template></xsl:stylesheet>*4: the above xslt generating this output*<?xml version="1.0" encoding="UTF-8"?> <POSMESSAGE> <TextMSG> This coupon is for a <emph>good guy</emph> whose first name is : </TextMSG> <TextMSG> </TextMSG> <TextMSG align="center"> <SIZE size="3"><emph>SADD</emph></SIZE> </TextMSG> <TextMSG align="center"> </TextMSG> <TextMSG align="right"> also whose full_name is Sadd Hossain </TextMSG> <TextMSG align="left"> </TextMSG> <TextMSG align="left"> He is a <SIZE size="5"><emph>software engineer</emph></SIZE> for S&H </TextMSG> </POSMESSAGE>notice that i have now <SIZE size="5"> but i really needed <SIZE ="5"> where I am doing the mistake????? If you could let me know.another last thing which i did not try yet as I could not solve this size issue is i have to transform any empty <p> with <LF> like in the above output where I have :<TextMSG></TextMSG>or<TextMSG align="left"></TextMSG>I have to replace it with just <LF/>or if you think it in terms of input<p></p>or<p align="left"></p>I have to replace it empty <p>s with <LF/>If you could shed any light about this also will be extremely helpful. thank you again

Link to comment
Share on other sites

You shouldn't be using empty <p> elements to create line breaks in your HTML document. If you actually want a line break, use a <br> element.XSLT won't know the difference between an empty <p> elements and a non-empty <p> element. In fact, it's not completely empty either, it has some whitespace in it.

Link to comment
Share on other sites

<SIZE ="3"> is not a valid XML construct. <SIZE> is an element, and the generated <SIZE size="3"> is really a "SIZE" element with a "size" attribute. You can't have a "null" attribute. I don't know if even SGML allows this construct, but even if it does - XML doesn't.You can detect empty paragraphs like:

<xsl:template match="p[not(*) and (text() = '' or normalize(text()) = ' ')"><LF/><xsl:template>

The first condition is going to make sure the element doesn't have any childs, the second is going to detect "<p/>" and "<p></p>", and the third is going to detect "<p> </p>" where there could be one or more whitespaces inside.That said, I second Ingolme in that you should use <br/> for line breaks instead of empty <p/> elements. If the input XML is under your control, consider switching.

Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...