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 October 10th, 2003, 08:36 AM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default xpath not picking up first element

I've been stumped on the for ages, please help.

I seem to have a problem with my XPath - the first element is not being picked up in the for-each loop when $grid_width = 3. Can anyone see why this is happening. The expression (../@xads_user_profile_id=exclusive/@xads_user_profile_id) in the for-each seems to be doing the damage.

Thanks in advance,

Darren Clump

XSLT - snip
-----------
The below is to create a table. When $grid_width = 5 there is no problem all the assets are picked up! However when the $grid_width = 3 the first asset is passed over!!

<xsl:for-each select="asset[(position() mod $grid_width = 1) and (@is_exclusive='true') and (../@xads_user_profile_id=exclusive/@xads_user_profile_id)]">
    <tr>
        <xsl:apply-templates select="(following-sibling::asset[(position() &lt; $grid_width)]) | ."/>
    </tr>
</xsl:for-each>

XML - snip
----------
<content xads_user_profile_id="1240871">
    <asset is_exclusive="true" content_type="image" label="KJFX007_NB" id="50190114" expired="false"/>
    <asset is_exclusive="true" content_type="image" label="KJ-FX-005CR" id="50190117" expired="false"/>
    <asset is_exclusive="true" content_type="image" label="KJ-FX-12" id="50666231" expired="false">
        <exclusive xads_user_profile_id="1240871" perm_level="50"/>
    </asset>
    <asset is_exclusive="true" content_type="image" label="KJ-FX-10" id="50666234" expired="false">
        <exclusive xads_user_profile_id="1240871" perm_level="50"/>
    </asset>
    <asset is_exclusive="true" content_type="image" label="KJ-1967" id="50666448" expired="false">
        <exclusive xads_user_profile_id="1240840" perm_level="10"/>
    </asset>
    <asset is_exclusive="true" content_type="image" label="KJ-1899" id="50666451" expired="false">
        <exclusive xads_user_profile_id="1240840" perm_level="10"/>
    </asset>
    <asset is_exclusive="true" content_type="image" label="KJ-3777" id="50670463" expired="false">
        <exclusive xads_user_profile_id="1240871" perm_level="50"/>
    </asset>
</content>
 
Old October 10th, 2003, 08:58 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Could you state what is needed. "position() mod 5" only equals 1 when the position equals 6, 11, 16 etc.

--

Joe
 
Old October 10th, 2003, 09:28 AM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

position()mod 5 (6,11,16..) - this is what the desired result is!! Hence if there are a certain number of assets a new row is made for the table.
The desired result for grid width = 3 if there are 5 and 8 desired assets is
<tr>
  <td>asset</td><td>asset</td><td>asset</td>
  <td>asset</td><td>asset</td><td><td>
</tr>
<tr>
  <td>asset</td><td>asset</td><td>asset</td>
  <td>asset</td><td>asset</td><td>asset</td>
  <td>asset</td><td>asset</td><td></td>
</tr>

the same priciple applies for grid_width=5



 
Old October 10th, 2003, 12:27 PM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<asset is_exclusive="true" content_type="image" label="KJ-FX-12" id="50666231" expired="false">
    <exclusive xads_user_profile_id="1240871" perm_level="50"/>
</asset>

is not getting picked up - but I want to display it!!

Thanks Darren
 
Old October 13th, 2003, 12:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,

First of all, the XPath expression
Code:
asset[position() mod $N = 1]
will give the nodes at positions 1, N + 1, 2*N + 1, 3*N + 1, etc.
So, in your example, when grid_width=3, according to your XSLT, you'll get the nodes from 4-th to 6-th as the result of one pass of the xsl:for-each (the first node is passed over, since the condition
Code:
../@xads_user_profile_id=exclusive/@xads_user_profile_id
is not met) and the 7-th node as the result of the second pass of the xsl:for-each. If you'll drop the condition I mentioned just above, you'll get all assets, since the first asset also will be included which in turn will "bring" assets standing at positions 2 and 3. So, if you want to see also the asset
Code:
<asset is_exclusive="true" content_type="image" label="KJ-FX-12" id="50666231" expired="false">
    <exclusive xads_user_profile_id="1240871" perm_level="50"/>
</asset>
you can drop that condition, for example.
But one point was strange for me: how can you get the first asset(by the same XSLT code you presented) when the grid_width=5?

Regards,
Armen
 
Old October 13th, 2003, 06:19 AM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry I think I've caused some confusion about which asset's I want to select.

With regards to the XML in the inital post, I want to match "assets" with @label (attribute) = KJ-FX-12, KJ-FX-10 and KJ-3777 (the assets that match the condition parent::content/@xads_user_profile_id=exclusive/@xads_user_profile_id).

When refering to the first asset - I ment the first desired asset (@label="KJ-FX-12").

if you wrap the below around the inital XSLT snip and run it against the initial XML snip (provided above) the problem will be apparent.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="content">
    <xsl:variable name="grid_width" select="3"/>
:
XSLT snip
:
</xsl:template>
<xsl:template match="asset">
    <td> asset name <xsl:value-of select="@label"/></td>
</xsl:template>
</xsl:stylesheet>

Thanks in advance,
Darren
 
Old October 13th, 2003, 09:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Ok, I hope I've understood what you want to do ;)

Here is the XSLT code:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="grid_width" select="3"/>

    <xsl:variable name="selected-assets" select="/content/asset[../@xads_user_profile_id=exclusive/@xads_user_profile_id]"/>

    <xsl:template match="content">
        <xsl:for-each select="$selected-assets[(position() mod $grid_width = 1) and (@is_exclusive='true')]">
            <xsl:variable name="curr-pos" select="position()"/>
            <xsl:value-of select="$curr-pos"/>
            <tr>                    
                <xsl:apply-templates select="$selected-assets[position() &gt;= $curr-pos and position() &lt; $curr-pos + $grid_width]"/>
            </tr>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="asset">
        <td>
            asset name: <xsl:value-of select="@label"/>
        </td>        
    </xsl:template>
</xsl:stylesheet>
I think the "following-sibling" axis confused you: it's applied on the original document order.

If this still isn't the result you meant, let me know.

Regards,
Armen
 
Old October 13th, 2003, 11:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

I forgot to remove the line

Code:
<xsl:value-of select="$curr-pos"/>
:D

It was there to show the current value...






Similar Threads
Thread Thread Starter Forum Replies Last Post
Picking mail from Inbox sanjepau Biztalk 0 July 28th, 2007 08:42 AM
Need help picking a class 38ffems Intro Programming 5 April 29th, 2007 11:14 AM
sorting and picking the first set salhabb XSLT 2 September 27th, 2005 04:13 AM
brand newbie - picking out values forkhead XML 2 August 19th, 2005 05:02 PM
Turning element text into XPath Kael XSLT 5 October 14th, 2004 03:37 PM





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