Hi All: I'm trying to use XSL to create a menu on a webpage. I want a particular subset of menu 'Item' nodes selected from the menu XML when I click on either (a) the parent of one of those items; (b) the direct descendant of one of those items; or (c) a self or sibling item. I can't ge开发者_StackOverflow中文版t my head around how to do this. Code fragments are at http://pastie.org/2072445
Oh, and it has to be XSLT 1.0 as I'm using the PHP parser.
You can't write XSLT code that tests if something has been clicked because the XSLT code runs to completion before the user gets a chance to click anything. You need to write XSLT code to generate HTML/Javascript code that responds to what is clicked. The best way of tackling this is to write an HTML/Javascript page that does what you want by hand; then you know what the output of your XSLT transformation needs to be. And it's much easier to write a program if you know what you want its output to be!
Kevin, I'm going to post some of your code here so I don't have to keeping switching over to pastie:
<!-- XSL CODE FRAGMENT -->
<xsl:choose>
<!-- If one of my ancestor items is the Active Item, then show myself and my siblings -->
<xsl:when test="boolean(ancestor::item[@id = $activeItemID])">
<xsl:apply-templates select="//submenu[@child_of = $activeItemID]"/>
</xsl:when>
<!-- If one of my child items is the Active Item, then show my siblings -->
<xsl:when test="boolean(descendant::item[@id = $activeItemID])">
<xsl:apply-templates
select="//submenu [@child_of = number(descendant::item[@id = $activeItemID]/parent::submenu/parent::item/@child_of)]"/>
</xsl:when>
<xsl:otherwise>
<!-- Either myself or one of my siblings is the active Item, so simply show myself and my siblings -->
<xsl:apply-templates
select="//submenu[@child_of = number(//item[@id = $activeItemID]/@child_of)]"/>
</xsl:otherwise>
</xsl:choose>
Since you haven't posted any of the surrounding XSLT, we have no way of knowing what the context item is here. For example, are you iterating through all item elements?? In other words, when the above comments say "my ancestor items", "my siblings", etc., we don't know what node is "me".
Then after using the context node to test for conditions relating to ancestors/descendants/siblings of the context node, you apply templates to submenu elements that oddly have no relation to the context node.
I'm really having a hard time figuring out what you're trying to do. In the sample code you are applying templates to submenu
elements; but in the comments you say you want to select item
elements.
In your comment to @Michael, you said "I need the xslt to return the node set that are children of submenu[@id = 1010]". For that, the code would be
<xsl:variable name="childNodes" select="//submenu[@id = 1010]/*" />
(I'm assuming you only care about the element children, not the (whitespace) text nodes.) This will give $childNodes a nodeset consisting of the four <item>
elements with ids 1016, 1017, 1019, 1020.
精彩评论