locked
web.sitemap and xpath RRS feed

  • Question

  • User-2006610559 posted

    I am attempting to write some code that will add new nodes to a sitemap file.  Not a problem.  However, I have added an id attribute to each siteMapNode:

    <siteMapNode url="sitemap.aspx" title="Nav" id="2">

    I am wanting to determine the max id in the file so that I can add 1 and assign the new value to the new node.  I am not certain how to write an xpath query to get the max id value.  Here is my full sitemap file.  It is just a simple test file.

     Thank you for any help that you can provide.

    <?xml version="1.0" encoding="utf-8"?>
    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
      <siteMapNode url="sitempa.aspx?id=1" title="Home" id="1">
        <siteMapNode url="sitempa.aspx?id=2" title="Nav One" id="2">
          <siteMapNode url="sitempa.aspx?id=3" title="Nav One Sub" id="3">
          </siteMapNode>
          <siteMapNode url="sitempa.aspx?id=10" title="test" id="10">
            <siteMapNode url="sitempa.aspx?id=11" title="test2" id="11" />
          </siteMapNode>
        </siteMapNode>
        <siteMapNode url="sitempa.aspx?id=4" title="Nav Two" id="4" />
      </siteMapNode>
    </siteMap>

    Monday, August 18, 2008 5:29 PM

Answers

  • User-1038969477 posted

    Hi ousooners,

    Find your answer here : http://forums.asp.net/p/1109320/1707973.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 21, 2008 3:07 AM
  • User851852702 posted

     This XSLT 1.0 solution was modified from this example: http://dotnetslackers.com/XML/re-31462_Getting_MAX_with_XSLT.aspx

     

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    	<xsl:template match="/">
    		<root>
    			<xsl:variable name="themax">
    				<xsl:call-template name="max">
    					<xsl:with-param name="list" select="//@id"/>
    				</xsl:call-template>
    			</xsl:variable>
    			<xsl:value-of select="$themax"/>
    		</root>
    	</xsl:template>
    	<xsl:template name="max">
    		<xsl:param name="list"/>
    		<xsl:choose>
    			<xsl:when test="$list">
    				<xsl:variable name="first" select="$list[1]"/>
    				<xsl:variable name="rest">
    					<xsl:call-template name="max">
    						<xsl:with-param name="list" select="$list[position() != 1]"/>
    					</xsl:call-template>
    				</xsl:variable>
    				<xsl:choose>
    					<xsl:when test="$first > $rest">
    						<xsl:value-of select="$first"/>
    					</xsl:when>
    					<xsl:otherwise>
    						<xsl:value-of select="$rest"/>
    					</xsl:otherwise>
    				</xsl:choose>
    			</xsl:when>
    			<xsl:otherwise>0</xsl:otherwise>
    		</xsl:choose>
    	</xsl:template>
    </xsl:stylesheet>
    
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 21, 2008 9:43 AM

All replies

  • User851852702 posted

     in xpath: count(//@id)

    Tuesday, August 19, 2008 9:27 AM
  • User-2006610559 posted

    Won't that just give a count and not the value of the max id?  I did try it and got an invalid token error.

    Wednesday, August 20, 2008 5:55 PM
  • User-1038969477 posted

    Hi ousooners,

    Find your answer here : http://forums.asp.net/p/1109320/1707973.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 21, 2008 3:07 AM
  • User851852702 posted

     This XSLT 1.0 solution was modified from this example: http://dotnetslackers.com/XML/re-31462_Getting_MAX_with_XSLT.aspx

     

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    	<xsl:template match="/">
    		<root>
    			<xsl:variable name="themax">
    				<xsl:call-template name="max">
    					<xsl:with-param name="list" select="//@id"/>
    				</xsl:call-template>
    			</xsl:variable>
    			<xsl:value-of select="$themax"/>
    		</root>
    	</xsl:template>
    	<xsl:template name="max">
    		<xsl:param name="list"/>
    		<xsl:choose>
    			<xsl:when test="$list">
    				<xsl:variable name="first" select="$list[1]"/>
    				<xsl:variable name="rest">
    					<xsl:call-template name="max">
    						<xsl:with-param name="list" select="$list[position() != 1]"/>
    					</xsl:call-template>
    				</xsl:variable>
    				<xsl:choose>
    					<xsl:when test="$first > $rest">
    						<xsl:value-of select="$first"/>
    					</xsl:when>
    					<xsl:otherwise>
    						<xsl:value-of select="$rest"/>
    					</xsl:otherwise>
    				</xsl:choose>
    			</xsl:when>
    			<xsl:otherwise>0</xsl:otherwise>
    		</xsl:choose>
    	</xsl:template>
    </xsl:stylesheet>
    
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 21, 2008 9:43 AM