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 August 11th, 2009, 07:28 PM
Registered User
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default need help with this html to xml transform

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 good guy whose first name is :
</TextMSG>

<TextMSG >

</TextMSG>

<TextMSG align="center">
<emph>SADD</emph>
</TextMSG>

<TextMSG >

</TextMSG>

<TextMSG align="right" >
also whose full_name is Sadd Hossain
</TextMSG>

<TextMSG>

</TextMSG>

<TextMSG align="left" >
He is a software engineer
for S&H
</TextMSG>

</POSMESSAGE>


*3. XSLT for this

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml"/>

<xsl:template match="body">

<POSMESSAGE>

<xsl:for-each select="p">

<TextMSG>

<!--xsl:if test="not@align=''"-->
<xsl:attribute name="align"><xsl:value-of select="@align"/></xsl:attribute>
<!--/xsl:if-->
<xsl:attribute name="font"><xsl:value-of select="@size"/></xsl:attribute>
<xsl:value-of select="."/>

</TextMSG>


<xsl:for-each select="b">

<emph>
<xsl:value-of select="."/>
</emph>

</xsl:for-each>

</xsl:for-each>

</POSMESSAGE>

</xsl:template>



</xsl:stylesheet>


*4: the above xslt generating this output*

<?xml version="1.0" encoding="UTF-8"?>



<POSMESSAGE><TextMSG align="" font="">
This coupon is for a good guy whose first name is :
</TextMSG><TextMSG align="" font="">

</TextMSG><TextMSG align="center" font="">
SADD
</TextMSG><TextMSG align="center" font="">

</TextMSG><TextMSG align="right" font="">
also whose full_name is Sadd Hossain
</TextMSG><TextMSG align="left" font="">

</TextMSG><TextMSG align="left" font="">
He is a software engineer
for S&H
</
*5: Need help with this. what should my xslt look like to get the desired output???????????????*

*any help or direction will be very much appreciated. Thank you*
 
Old August 12th, 2009, 08:12 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a sample XSLT 2.0 stylesheet:
Code:
<xsl:stylesheet
  xmlns: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="text()[normalize-space(.) = 'Sadd']">
    <emph><xsl:value-of select="upper-case(.)"/></emph>
  </xsl:template>
  
</xsl:stylesheet>
When applied to the input
Code:
<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&amp;H</font>
</p>
</body>
</html>
Saxon 9 creates the following output:
Code:
<?xml version="1.0" encoding="UTF-8"?>



<POSMESSAGE>
<TextMSG>
This coupon is for a good guy whose first name is :
</TextMSG>
<TextMSG>

</TextMSG>
<TextMSG align="center"><emph>
SADD
</emph></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 software engineer for
S&amp;H
</TextMSG>
</POSMESSAGE>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 12th, 2009, 06:33 PM
Registered User
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Wow Martin Thanks but still want to ask more

Martin you just made this so easy. Thanks a lot. but I have some more issues. I dont know whether it would be appropriate to ask that much but I will try.

one part of your xsl is like this:

<xsl:template match="text()[normalize-space(.) = 'Sadd']">
<emph><xsl:value-of select="upper-case(.)"/></emph>
</xsl:template>


here you were transforming "Sadd" to "SADD" with a <emph> tag. actually my html is not static. I actually have a SWING text editor(almost like this text editor where I am writing this message now ) where you can write text coupon and in the server side I was getting the html. I was trying to transorm that html. that html might have anything. So, actually I was needing to transform any <b> tag with <emph> tag wherever it appears. I really dont need the uppercase thing. So, following your example I replaced the above part of your code with this:

<xsl:template match="b">
<emph>
<xsl:copy-of select="@"/>
<xsl:apply-templates/>
</emph>
</xsl:template>

So, my new xsl looks like this now(which is again brainchild of yours):

<xsl:stylesheet
xmlns: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:stylesheet>



after using the above xsl my output is :

<POSMESSAGE>
<TextMSG>
This coupon is for a <emph>good guy</emph> whose first name is :
</TextMSG>
<TextMSG>

</TextMSG>

<TextMSG align="center">
<emph>Sadd</emph>
</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 <emph>software engineer</emph> for S&amp;H
</TextMSG>
</POSMESSAGE>


That is perfect. but one thing is I have to transform the font size attribute also from my original input. initially i ignored that. As now you gave me a start I need to do this too. I need "SIZE" instead of "font size" in the output. So, i followed your way and added this snippet with the existing:

<xsl:template match="font size">
<SIZE>
<xsl:copy-of select="@size"/>
<xsl:apply-templates/>
</SIZE>
</xsl:template>


So, my final xsl look like this now:

<xsl:stylesheet
xmlns: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>


which gives me 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&amp;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. even if you cant still I want to Thank you again. your help was really really helpful. I was really going in a wrong way regarding XSL before your help.
 
Old August 12th, 2009, 06:45 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>*notice that i have now <SIZE size="5"> but i really needed
> <SIZE ="5"> where I am doing the mistake?????

<SIZE="5"> isn't well-formed XML, so you can't generate it using XSLT.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 13th, 2009, 07:07 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

To replace empty p elements with LF you can add a template
Code:
<xsl:template match="p[not(normalize-space())]">
  <LF/>
<xsl:template>
at the end of your stylesheet.

As for the other requirement with the SIZE, as has already been pointed out, what you describe as the desired result, is not XML so it is not possible to achieve that.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform xml to xml changing one tag. surfer97301 XSLT 2 April 21st, 2010 05:14 PM
XML to XML transform (Simplified) nmahesh567 XSLT 2 March 24th, 2007 07:57 AM
Transform html table to add ids/headers using xsl kapy_kal XSLT 2 February 21st, 2007 10:12 AM
transform a xml to a html table robert_trudel_fr XSLT 3 December 3rd, 2006 02:16 PM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM





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