Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT Matching Tree from Different Nodes

Status
Not open for further replies.

jrapp

Programmer
May 15, 2003
25
0
0
US
I'm trying to output a category tree/sitemap to HTML using XML & XSLT. The XML gives me a list of categories and the current selected category in two different nodes. I need to grab the category node from the tree that corresponds to selected category. Here's a sample XML:

Code:
<Page>
   <CategoryTree>
      <CurrentCategoryID>0</CurrentCategoryID>
      <Categories>
        <CategoryID>0</CategoryID>
        <CategoryName>__parent</CategoryName>
        <ChildCategories>
<Category>
	<CategoryID>31</CategoryID>
	<CategoryName>My Category</CategoryName>
	<ChildCategories>
	  <Category>
		<CategoryID>39</CategoryID>
		<CategoryName>Child 1</CategoryName>
		<ChildCategories />
	  </Category>
	  <Category>
		<CategoryID>42</CategoryID>
		<CategoryName>Child2</CategoryName>
		<ChildCategories />
	  </Category>
	</ChildCategories>
  </Category>
        </ChildCategories>
      </Categories>
   </CategoryTree>
    <BrowseCurrentCategory>
      <CategoryID>31</CategoryID>
    </BrowseCurrentCategory>
</Page>

I can rock it with an xpath that looks like:
Code:
/Page/CategoryTree//[CategoryID=31]

but what I really need is:
Code:
/Page/CategoryTree//[CategoryID=/Page/BrowseCurrentCategory/CategoryID]
however, that just ends up matching everything. Anybody know why? Thank you, thank you!
 
You would probably make your life simpler, and the XPath expression easier to read, if you used a variable.
Code:
<xsl:variable name="desiredCategory" select="/Page/BrowseCurrentCategory"/>
<xsl:for-each select="/Page/Categories//Category[CategoryId=$desiredCategory]">
...
</xsl:for-each>
The XPath expression in the xsl:for-each (given as a usage example) will clearly only select <Category> elements, and in particular, only those <Category> elements that have a child <CategoryID> element whose value is equal to the value of $desiredCategory set in the previous processing instruction.

Tom Morrison
 
I like it! I'll give it a try. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top