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 3rd, 2006, 11:49 AM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL Test

Given XML like this:

Code:
<nav>
    <tree>
        <page id="5004" desc="Site" sort="5004">
            <page id="5242" desc="Careers" sort="5242">
                <page id="350231838" desc="Careers Update" sort="350231838">
                    <page id="350231839" desc="September 2005" sort="350231839" />
                </page>    
            </page>
        </page>
    </tree>
    <hidden>
        <page id="350231839" />
    </hidden>
</nav>
I want to transform the pages (please ignore the badly formed XML) into a series of unordered lists, but I don't want to output any elements if they appear in the <hidden> section. My current test before I output the ul is xsl:if "child::page", but this does not properly handle the case where the current element has children, but they all appear in the hidden section. How would I ask (essentially), "If this element has at least one child element 'page' that does not show up in the hidden list"?

 
Old February 3rd, 2006, 12:35 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Think you need the not function:
Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>

  <xsl:template match="/">
    <html>
      <body>
        [list]
          <xsl:apply-templates select="nav/tree/page[not(@id = /nav/hidden/page/@id)]" />
        </ul>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="page">
    <li>
      <xsl:value-of select="@desc"/>
      <xsl:if test="page[not(@id = /nav/hidden/page/@id)]">
        [list]
          <xsl:apply-templates select="page[not(@id = /nav/hidden/page/@id)]"/>
        </ul>
      </xsl:if>
    </li>
  </xsl:template>
</xsl:stylesheet>
This assumes that when you hit a hidden item it has no more visible children.

--

Joe (Microsoft MVP - XML)
 
Old February 3rd, 2006, 02:49 PM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perfect. Thanks. I didn't know how to reference elements in that way.






Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:test, but ignoring whitespace mphare XSLT 1 December 5th, 2008 05:23 AM
XSL Params inside When Test shasto100 XSLT 3 April 21st, 2006 05:45 AM
Sorting a xsl:if test="position()" charmaigne XSLT 2 April 12th, 2006 04:42 AM
<xsl:if test="" Question kwilliams XSLT 2 August 9th, 2005 08:30 AM
Using xsl:if test attributes rdavisiii XSLT 4 November 9th, 2004 12:24 AM





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