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 21st, 2006, 02:49 PM
Registered User
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to azrail1222
Default Deep element access

I am new to this whole XSLT thing, however I love it lots so far. I am getting confused when it comes to accessing parameters to XML nested within others.

XML file to be transformed (simplified)
Code:
<form width="60%" align="center" method="post" action="request.php">
  <line>
    <desc required="true">Name:</desc>
    <entry>
      <input type="text" name="name" size="50" maxlength="50" value="<?=$name?>"/>
    </entry>
  </line>

  <line>
    <desc/>
    <entry align="center">
      Full: [First Middle Last]
    </entry>
  </line>
</form>



The XSLT for the transformation.

Code:
    <xsl:template match="body//form">
        <form method="" action="">
            <xsl:if test="@method">
                <xsl:attribute name="method">
                    <xsl:value-of select="@method"/>
                </xsl:attribute>
            </xsl:if>

            <xsl:if test="@action">
                <xsl:attribute name="action">
                    <xsl:value-of select="@action"/>
                </xsl:attribute>
            </xsl:if>


            <table width="100%" align="center">
                <xsl:if test="@width">
                    <xsl:attribute name="width">
                        <xsl:value-of select="@width"/>
                    </xsl:attribute>
                </xsl:if>

                <xsl:if test="@align">
                    <xsl:attribute name="align">
                        <xsl:value-of select="@align"/>
                    </xsl:attribute>
                </xsl:if>

                <xsl:apply-templates select="node()[not(self::line)]|@*|text()"/>                      
                <xsl:for-each select="line">
                    <tr>    
                        <xsl:call-template name="desc"/>
                        <xsl:call-template name="entry"/>
                    </tr>   
                </xsl:for-each>

            </table>
        </form> 
    </xsl:template>

    <xsl:template name="desc">
        <td align="left">
   ---->    <xsl:if test="@required = true">
                <star/> 
            </xsl:if>
            <xsl:apply-templates select="desc"/>    
        </td>   
    </xsl:template>

    <xsl:template name="entry">
        <td align="right">
  ----->    <xsl:if test="@align">
                <xsl:attribute name="align">
                    <xsl:value-of select="@align"/>
                </xsl:attribute>
            </xsl:if>

            <xsl:apply-templates select="entry"/>   
        </td>   
    </xsl:template>
The problem I am having is that within the 'desc' and 'entry' templates the if's for the parameters are never being called.

I assumed since:
<desc required="true">

that

<xsl:if test="@required = true">

Should work, but it's not. Why and how do I fix it?

--
dave

 
Old August 22nd, 2006, 02:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, this code:

                <xsl:if test="@width">
                    <xsl:attribute name="width">
                        <xsl:value-of select="@width"/>
                    </xsl:attribute>
                </xsl:if>

                <xsl:if test="@align">
                    <xsl:attribute name="align">
                        <xsl:value-of select="@align"/>
                    </xsl:attribute>
                </xsl:if>

can be reduced to:

    <xsl:copy-of select="@width | @align"/>

Secondly, because you are using call-template rather than apply-templates to process desc and entry, the context node isn't changed -it's still the "line" element, and this element doesn't have the attributes you are looking for. Use apply-templates instead.



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
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
please help deep nested looping rsf0031 XSLT 7 June 20th, 2007 04:03 PM
Access specific Element and its Child elements! suersh79 XML 3 November 22nd, 2006 12:58 AM
Nested Repeaters three deep? Silfverduk ASP.NET 2.0 Basics 0 June 21st, 2006 01:14 PM
AS/400 and VB6.0 - Deep in the “ol” Kim chi!!!! kf4rrm Pro VB Databases 0 March 14th, 2005 11:37 AM





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