Trying to write breadcrumb navigation using XML/XSL
I am trying to write a breadcrumb navigation by using XSL to transform XML.
Here is a piece of the XML file I am using:
<Menu>
<ID>22486a60-2bfe-46ba-96ee-7ea92b6ca5bb</ID>
<PageID>00000000-0000-0000-0000-000000000000</PageID>
<Link/>
<Target/>
<Title>Main Menu</Title>
<Image/>
<Items>
<Menu>
<ID>44595707-a331-49ae-a8ed-4bc1cef22e4f</ID>
<PageID>83a1df5f-2909-4c8a-9aa7-a4f2e0111d7c</PageID>
<Link>/home.aspx</Link>
<Target>_self</Target>
<Title>Home</Title>
<Image/>
<Items/>
</Menu>
<Menu>
<ID>a9fd822b-838f-4129-9628-4ce667005450</ID>
<PageID>88e8e3c3-39e3-40a4-9dfd-476f2082f4e0</PageID>
<Link>/About-Us.aspx</Link>
<Target>_self</Target>
<Title>About Us</Title>
<Image/>
<Items/>
</Menu>
<Menu>
<ID>3aa256eb-9f80-42b3-b637-2e22f7008adb</ID>
<PageID>3369b394-1aec-4a9f-bb9f-1bae7bdde858</PageID>
<Link>/Products.aspx</Link>
<Target>_self</Target>
<Title>Products</Title>
<Image/>
<Items>
<Menu>
<ID>b2f7efae-19d7-4177-9874-962944af9836</ID>
<PageID>88e8e3c3-39e3-40a4-9dfd-476f2082f4e0</PageID>
<Link>/Aprons.aspx</Link>
<Target>_self</Target>
<Title>Aprons</Title>
<Image/>
<Items/>
</Menu>开发者_如何学Python
I am having trouble getting the XSL to display the current page. It displays all the ancestors but not the current page. Here is what I've come up with so far:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:param name="CurrentPage">a5e2418a-f42e-4c0d-9d63-515ecb6af79e</xsl:param>
<xsl:template match="/">
<ul id="breadcrumb">
<xsl:call-template name="innerNode">
<!--<xsl:value-of select="ancestor-or-self::PageID = $CurrentPage"/>-->
</xsl:call-template>
</ul>
</xsl:template>
<xsl:template name="innerNode">
<xsl:for-each select="Menu/Items">
<xsl:if test="descendant::*/PageID = $CurrentPage">
<li>
<xsl:choose>
<xsl:when test="not(PageID = $CurrentPage)">
<a href="/"><xsl:value-of select="../Title"/></a>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Items"/>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:if>
<xsl:call-template name="innerNode"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Any help would be appreciated!
Thanks, Jason
The following stylesheet first selects the menu item with the correct page ID, then displays all its ancestors, and finally displays the menu item itself. This approach is more effective than repeatedly calculating the expression descendant::*
.
Note that if the page ID does not exist in the menu structure, no ul
element is output. If you want to display the ul
element even if it will be empty, change the stylesheet so that ul
wraps xsl:for-each
.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:param name="CurrentPage">a5e2418a-f42e-4c0d-9d63-515ecb6af79e</xsl:param>
<xsl:template match="/">
<xsl:for-each select="//Menu[PageID = $CurrentPage]">
<ul id="breadcrumb">
<xsl:for-each select="ancestor::Menu">
<li><a href="{Link}"><xsl:value-of select="Title"/></a></li>
</xsl:for-each>
<li><xsl:value-of select="Title"/></li>
</ul>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
精彩评论