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 27th, 2010, 02:16 AM
Authorized User
 
Join Date: May 2008
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rangeshram
Default Display HTML Attributes from XML Attributes

Dear All,

We want to use the HTML paragraph attribute based on the xml attribute from the element <rx.p>

XML:
<rx.p justify="full" left-indent="2" first-indent="1"><rx.text>In the event either (a) no such appeal is made by the Purchaser within fourteen (14) working days from the date of receipt by the Purchaser.</rx.text></rx.p>

We're using the below xslt using SAXAN and XSLT2.0 (Oxygen Editor):
XSLT:
Code:
 
<xsl:template match="rx.p">
        <xsl:variable name="Findent"><xsl:value-of select="@first-indent"/></xsl:variable>
        <xsl:variable name="Lindent"><xsl:value-of select="@left-indent"/></xsl:variable> 
 <xsl:choose>
                <xsl:when test="@justify='full'">
                    <p align="justify">
                        <xsl:if test="$Findent = '1'">
                            <xsl:attribute name="style">text-indent:.3in;</xsl:attribute>
                         </xsl:if>
                        <xsl:if test="$Lindent = '1'">
                            <xsl:attribute name="style">margin-left:.3in;</xsl:attribute>
                         </xsl:if>
 
                        <xsl:if test="$Findent = '1.5'">
                            <xsl:attribute name="style">text-indent:.4.5in;</xsl:attribute>
                         </xsl:if>
                        <xsl:if test="$Lindent = '1.5'">
                            <xsl:attribute name="style">margin-left:.4.5in;</xsl:attribute>
                         </xsl:if>
 
                        <xsl:if test="$Findent = '2'">
                            <xsl:attribute name="style">text-indent:.6in;</xsl:attribute>
                         </xsl:if>
                        <xsl:if test="$Lindent = '2'">
                            <xsl:attribute name="style">margin-left:.6in;</xsl:attribute>
                         </xsl:if>
 
                       <xsl:if test="$Findent = '2.5'">
                            <xsl:attribute name="style">text-indent:.7.5in;</xsl:attribute>
                         </xsl:if>                    
                        <xsl:if test="$Lindent = '2.5'">
                            <xsl:attribute name="style">margin-left:.7.5in;</xsl:attribute>
                         </xsl:if>
 
                        <xsl:if test="$Findent = '3'">
                            <xsl:attribute name="style">text-indent:.9in;</xsl:attribute>
                         </xsl:if>                    
                        <xsl:if test="$Lindent = '3'">
                            <xsl:attribute name="style">margin-left:.9in;</xsl:attribute>
                         </xsl:if>
 
                       <xsl:apply-templates/>
                    </p>    
                </xsl:when>
</xsl:choose>
Using above XSLT the output HTML:
<p align="justify" style="margin-left:.6in;">In the event either (a) no such appeal is made by the Purchaser within fourteen (14) working days from the date of receipt by the Purchaser.</p>

The problem in the above HTML is only the attribute "margin-left" is there but not "text-indent". Since we're new to this Transformation please guide us to modify the above code and generates the HTML as below.

Required HTML:
<p align="justify" style="margin-left:.6in; text-indent:.3in">In the event either (a) no such appeal is made by the Purchaser within fourteen (14) working days from the date of receipt by the Purchaser.</p>

Can someone please help...
 
Old March 27th, 2010, 05:02 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The reason you are only seeing one of the style properties is because the second style attribute is overwriting the first one, not adding to it.

If the @first-indent and @left-indent attributes are always there then following would be much simpler:

Code:
<p align="justify" style="margin-left:.{@left-indent*3}in;text-indent:.{@first-indent*3}in;">
    <xsl:apply-templates/>
</p>
If one or the other is likely to not exist then you'd have to use <xsl:attribute> ut put all your processing inside the one attribute:

Code:
<p align="justify">
    <xsl:attribute name="style">
        <xsl:if test="@left-indent">margin-left:.<xsl:value-of select="@left-indent*3"/>in;</xsl:if>
        <xsl:if test="@first-indent">text-indent:.<xsl:value-of select="@first-indent*3"/>in;</xsl:if>
    </xsl:attribute>
    <xsl:apply-templates/>
</p>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 27th, 2010, 05:13 AM
Authorized User
 
Join Date: May 2008
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rangeshram
Default

Thanks Samjudson for your code and explanation. It's works as expected here...
 
Old March 27th, 2010, 01:14 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

One other point. Don't do this:

Code:
<xsl:variable name="Findent"><xsl:value-of select="@first-indent"/></xsl:variable>
It's much simpler and more efficient to write:

Code:
<xsl:variable name="Findent" select="@first-indent"/>
In the first case you are constructing a new XML tree; in the second case the variable is simply a reference to an existing node in an existing tree.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving xml attributes christianhau Classic ASP XML 0 July 19th, 2007 06:25 AM
Using xml elements as html attributes chipmaster XSLT 4 June 26th, 2007 10:22 AM
need html attribute based on xml attributes charles95621 XSLT 1 May 24th, 2007 05:21 PM
Odd attributes on Web- and Html Controls jacob ASP.NET 1.0 and 1.1 Professional 4 November 6th, 2005 07:24 AM
XML attributes miguel.ossa C# 2 February 9th, 2004 07:59 AM





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