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 May 24th, 2007, 01:44 PM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default need html attribute based on xml attributes

I have been given a book in xml to translate to xhtml. I have successfully met all the challenges but I have a big one still faceing me. I have to overide defaults in my css stylesheet. The best way to do that is to set the style attribute on the division. There will only be top margin, bottom margin, font color, and background color. This means that I have to build a string containing the desired CSS. I haven't had to use vaiables or parmeters to this point but I see no other way in this case.
I have made several attempts and I think I am close. The variable is getting built on the first match and I get the desired output. The scope changes (I think) and it works until I get another highlight box that needs to change.

The XML I am given looks like:

Code:
 <HighlightBox topmargin="25px" bottom-margin="25px">
    <p>
       Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut quis libero. Vivamus mollis. Aliquam eu eros. Nam varius lacus ut est. In hac habitasse platea dictumst. Suspendisse at velit in nulla semper fringilla. Suspendisse aliquam euismod tortor. Duis diam tortor, aliquam nec, aliquam ut, pharetra ac, purus. Nullam hendrerit. Etiam at metus quis lectus posuere venenatis. Quisque luctus scelerisque nunc. Fusce ultricies. Fusce vitae diam. Donec non urna quis lectus faucibus pretium. Nullam sed odio sit amet dolor tincidunt fringilla. Mauris eu tortor in ante commodo sodales. Morbi at arcu ut quam euismod elementum. Fusce libero leo, dictum vel, ultricies id, consectetuer a, nulla.

   </p>
</HighlightBox>
The output would be

Code:
<div class="highlightbox" style="margin-top:25px; margin-bottom:25px">

   <p>.....</p>
</div>
This is what I have come up with thus far:

Code:
xsl:template match="HighlightBox">
    <xsl:variable name="styleString">
        <xsl:element name="TempStyle">
            <xsl:for-each select="@*">
                <xsl:element name="StyleTMargin">
                    <xsl:if test="local-name()='topmargin'">
                       <xsl:value-of
                         select="concat('margin-top:',.,';')"/>
                    </xsl:if>
                </xsl:element>
                <xsl:element name="StyleBMargin">
                    <xsl:if test="local-name()='bottom-margin'">
                        <xsl:value-of
                         select="concat('margin-bottom:',.,';')"/>
                    </xsl:if>
                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:variable>
    <div>
        <xsl:attribute name="style">
            <xsl:value-of 
            select="concat(msxsl:node-set($styleString)/TempStyle/StyleTMargin,msxsl:node-set($styleString)/TempStyle/StyleBMargin)"/>
        </xsl:attribute>
    </div>
</xsl:template>
 
Old May 24th, 2007, 05:21 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can't see why you're making this so difficult. It looks to me like:

<xsl:template match="HighlightBox">
  <div>
    <xsl:attribute name="style">
      <xsl:if test="@topmargin">
        <xsl:text>margin-top:</xsl:text>
        <xsl:value-of select="@topmargin"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@bottom-margin">
        <xsl:text>margin-bottom:</xsl:text>
        <xsl:value-of select="@bottom-margin"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:attribute>
    <xsl:apply-templates/>
  </div>
</xsl:template>

But then, I'm not sure what's wrong with your current code, other than being excessively complicated. What output is it producing, and how does it differ from the output that you want?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Default Rows Based Upon Number of XML Attributes. Mr.T Infopath 0 November 5th, 2007 07:29 PM
Using xml elements as html attributes chipmaster XSLT 4 June 26th, 2007 10:22 AM
Grouping based on attributes values Chamkaur XSLT 4 June 21st, 2006 05:51 AM
Rules based on attribute values... jacob XML 2 July 6th, 2005 01:50 PM
conditional statement based on attribute value dancbishop XSLT 2 November 7th, 2003 12:50 PM





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