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 February 21st, 2006, 07:41 PM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Nodeset as Parameter...

Hello!

May be an easy question for this community...

I am trying to pass a nodeset as a parameter using call-template, initially, all the elements in xml were at the same level, like this:

<TextBox1/>
<Image/>
<ItemLinkName/>
<ItemTopic/>

and I was using: ./following-sibling::*

Now, I have elements like:
<RepeatedItems>
  <RepeatedItem>
    <RepeatedLinks>
      <ItemLinkName>eg1</ItemLinkName>
      <ItemTopic>Item1</ItemTopic>
    </RepeatedLinks>
  </RepeatedItem>
</RepeatedItems>

How do I get siblings for ItemLinkName in this case?

Appreciate any help on this. Thanks!
 
Old February 22nd, 2006, 04:21 PM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is this not possible?

 
Old February 22nd, 2006, 05:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Ye, it's perfectly possible, but I didn't feel I understood either your problem or your difficulty in solving it well enough to say anything useful.

If you don't get a reply to a question it means that people who read it either didn't understand the question or didn't know the answer. Asking the same question again isn't helpful. General guidelines for questions:

* show your input
* show your desired output
* if not obvious, explain how the two relate
* keep the above small (preferably 10 lines max)
* explain what you've tried and how this differed from the desired result
* try to make it clear how much knowledge you have

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 22nd, 2006, 06:20 PM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm I always try to give as much infomation as possible, but never reach that line. I will try to add more info but don't know if I can provide everything...

Old Input:
<Content>
    <Image/>
    <ItemTopic/>
    <ItemLinkName/>
    <ItemTopic/>
    <ItemLinkName/>

    <Image/>
    <ItemTopic/>
    <ItemLinkName/>
    <ItemTopic/>
    <ItemLinkName/>
</Content>

Old XSL:
I used this function to display all "ItemTopic"s and "ItemLinkName"s. "nodeset" parameter has the information for

<xsl:for-each select="Image">
  <xsl:if test="(./following-sibling::ItemTopic[1]!= '')">
    <xsl:call-template name="DisplayRepeatingLinks">
        <xsl:with-param name="nodeset" select="following-sibling::*"/>
        <xsl:with-param name="PrimaryItemTopic" select="'ItemTopic'"/>
        <xsl:with-param name="PrimaryItemLinkName" select="'ItemLinkName'"/>
        <xsl:with-param name="stopElementName" select="'Image'"/>
    </xsl:call-template>
  </xsl:if>
</xsl:for-each>

Output:
<table>
    <tr>
        <td>Image:</td>
    </tr>
</table>
<table>
    <tr>
        <td>
            <a href="ItemLinkName1">ItemTopic1</a>
        </td>
    </tr>
    <tr>
        <td>
            <a href="ItemLinkName2">ItemTopic2</a>
        </td>
    </tr>
</table>


Now the input changed to:

<RepeatedItems>
  <RepeatedItem>
    <Image>Image1</Image>

    <RepeatedLinks>
      <ItemTopic>ItemTopic</ItemTopic>
      <ItemLinkName>ItemLinkName1</ItemLinkName>
      <ItemTopic>ItemTopic2</ItemTopic>
      <ItemLinkName>ItemLinkName2</ItemLinkName>
    </RepeatedLinks>

    <Image>Image2</Image>

    <RepeatedLinks>
      <ItemTopic>ItemTopic</ItemTopic>
      <ItemLinkName>ItemLinkName1</ItemLinkName>
      <ItemTopic>ItemTopic2</ItemTopic>
      <ItemLinkName>ItemLinkName2</ItemLinkName>
    </RepeatedLinks>
  </RepeatedItem>
</RepeatedItems>

Now, how do I use call-template and pass the nodeset parameter to get the exact output as before? Nodeset should be pointing to that ItemTopic node.

I know the infomation is soooo incomplete, but if I paste everything it will be 15 to 20 pages.

Please reply, if I am not making any sense... Thanks again!
 
Old February 23rd, 2006, 06:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't shown the called template. Are you saying you can't change the code of template DisplayRepeatingLinks? In that case the answer depends on what assumptions the called template makes about the data structure, which I can't tell without seeing it.

Now you have a (slightly) cleaner XML structure, the best answer is to pass the single node RepeatedLinks as a parameter, and have the called template navigate to its children. This would be with-param select="following-sibling::RepeatedLinks[1]". But you could, if you prefer, locate the children in the calling code, by doing select="following-sibling::RepeatedLinks[1]/*"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 23rd, 2006, 06:33 PM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sure it does not make any sense without seeing the template code. Here it goes:

<xsl:template name="DisplayRepeatingLinks">
<xsl:param name="nodeset"/>
<xsl:param name="linkNameElementName"/>
<xsl:param name="linkPrimaryURLElementName"/>
<xsl:param name="linkSecondaryURLElementName"/>
<xsl:param name="stopElementName"/>

<xsl:if test="name($nodeset[1]) = $linkNameElementName">
    <xsl:if test="$nodeset[1] != '' ">
        <xsl:call-template name="DisplayLink">
            <xsl:with-param name="linkNameElement" select="$nodeset[1]"/>
            <xsl:with-param name="linkPrimaryURLElementName" select="$linkPrimaryURLElementName"/>
            <xsl:with-param name="linkSecondaryURLElementName" select="$linkSecondaryURLElementName"/>
            <xsl:with-param name="linkClass" select="$linkClass"/>
            <xsl:with-param name="stopElementName" select="$stopElementName"/>
        </xsl:call-template>
        <br/>
    </xsl:if>
</xsl:if>

<xsl:if test="name($nodeset[1]) != $stopElementName">

    <xsl:if test="count($nodeset[1]/following-sibling::*) != 0">
        <xsl:call-template name="DisplayRepeatingLinks">
            <xsl:with-param name="nodeset" select="$nodeset[1]/following-sibling::*"/>
            <xsl:with-param name="linkNameElementName" select="$linkNameElementName"/>
            <xsl:with-param name="linkPrimaryURLElementName" select="$linkPrimaryURLElementName"/>
            <xsl:with-param name="linkSecondaryURLElementName" select="$linkSecondaryURLElementName"/>
            <xsl:with-param name="stopElementName" select="$stopElementName"/>
        </xsl:call-template>
    </xsl:if>
</xsl:if>
</xsl:template>


May be you don't need DisplayLink function but I'll paste it any way:
<xsl:template name="DisplayLink">
<xsl:param name="linkNameElement"/>
<xsl:param name="linkPrimaryURLElementName"/>
<xsl:param name="linkSecondaryURLElementName"/>
<xsl:param name="linkClass" select="'blueLink'"/>
<xsl:param name="stopElementName"/>

<xsl:variable name="primaryURL">
    <xsl:call-template name="GetNextNamedElement">
        <xsl:with-param name="desiredElementName" select="$linkPrimaryURLElementName"/>
        <xsl:with-param name="stopElementName" select="$stopElementName"/>
        <xsl:with-param name="nodeset" select="$linkNameElement/following-sibling::*"/>
    </xsl:call-template>
</xsl:variable>

<xsl:variable name="secondaryURL">
    <xsl:call-template name="GetNextNamedElement">
        <xsl:with-param name="desiredElementName" select="$linkSecondaryURLElementName"/>
        <xsl:with-param name="stopElementName" select="$stopElementName"/>
        <xsl:with-param name="nodeset" select="$linkNameElement/following-sibling::*"/>
    </xsl:call-template>
</xsl:variable>
<!-- We need to determine which link to use when displaying the URL. If the
primary exists, we use it, otherwise we use the secondary URL. -->
<xsl:variable name="linkURL">
    <xsl:choose>
        <xsl:when test="$primaryURL != ''">
            <xsl:value-of select="$primaryURL"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$secondaryURL"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
<!-- Now we need to figure out if this link is a new window or a standard
link -->
<xsl:variable name="newWindow">
    <xsl:choose>
        <xsl:when test="substring($linkURL,1,1) = '*'">
            <xsl:value-of select="'true'"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="'false'"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<xsl:choose>
    <xsl:when test="$newWindow = 'true'">
        <a href="{substring-after($linkURL,'*')}" target="_new" class="{$linkClass}">
            <xsl:value-of select="$linkNameElement"/>
        </a>
    </xsl:when>
    <xsl:otherwise>
        <a href="{$linkURL}" class="{$linkClass}">
            <xsl:value-of select="$linkNameElement"/>
        </a>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="GetNextNamedElement">
<xsl:param name="desiredElementName"/>
<xsl:param name="stopElementName"/>
<xsl:param name="nodeset"/>
<xsl:choose>

    <xsl:when test="$desiredElementName = name($nodeset[1])">
        <xsl:apply-templates select="$nodeset[1]"/>
    </xsl:when>

    <xsl:when test="$stopElementName = name($nodeset[1])">
        <xsl:value-of select="''"/>
    </xsl:when>

    <xsl:when test="count($nodeset[1]/following-sibling::*) = 0">
        <xsl:value-of select="''"/>
    </xsl:when>

    <xsl:otherwise>
        <xsl:call-template name="GetNextNamedElement">
            <xsl:with-param name="desiredElementName" select="$desiredElementName"/>
            <xsl:with-param name="stopElementName" select="$stopElementName"/>
            <xsl:with-param name="nodeset" select="$nodeset[1]/following-sibling::*"/>
        </xsl:call-template>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>




This code was written for the first content style, that is why it is using StopElement for counting Links. I am sure this is not an excellent coding practice, so, you are welcome to comment and suggest a better standard!

 
Old February 24th, 2006, 03:07 PM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am sure there are plenty of solutions but because the elements were in a structure, I was able to get the out put like this:

<xsl:for-each select="./following-sibling::RepeatedLinks">
    <xsl:variable name="linkNameElement" select="ItemLinkName"/>
    <xsl:variable name="linkURL" select="ItemLinkURL"/>

    <xsl:variable name="newWindow">
        <xsl:choose>
            <xsl:when test="substring($linkURL,1,1) = '*'">
                <xsl:value-of select="'true'"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="'false'"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:choose>
        <xsl:when test="$newWindow = 'true'">
            <a href="{substring-after($linkURL,'*')}" target="_new" class="blueLink">
                <xsl:value-of select="$linkNameElement"/>
            </a>
        </xsl:when>
        <xsl:otherwise>
            <a href="{$linkURL}" class="blueLink">
                <xsl:value-of select="$linkNameElement"/>
            </a>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>





Similar Threads
Thread Thread Starter Forum Replies Last Post
select nodeset newbies1234 XSLT 4 August 22nd, 2008 10:52 AM
Reversing nodeset sudhish.sikhamani XSLT 3 June 19th, 2008 11:43 AM
with-param nodeset RoeZ XSLT 6 November 1st, 2007 06:53 PM
comparing nodeset stolte XSLT 7 May 30th, 2007 03:58 PM
How to create and return a nodeset in a javascript jerry.gadd XSLT 8 October 17th, 2003 05:48 AM





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