tags:

views:

36

answers:

1

Dimitre - great stuff! You nailed the xml schema and my expected result.

I am using a xslt fragment to pull this information into the page. I am passing my pageIds into the xslt via the xslt fragment call. How can extend this code be able to process those passed in parameters in order to display the correct data from the xml?

This is a fragment example.

<%    
    Dim mm_xsl20 As MM.XSLTransform = New MM.XSLTransform()
    mm_xsl20.setXML(Server.MapPath("/" & countryVar & "/" & langVar & "/titles.xml"))
    mm_xsl20.setXSL(Server.MapPath("/data/xslt/breadcrumb.xsl"))
    mm_xsl20.addParameter("pageID1", "hpPage")
    mm_xsl20.addParameter("pageID1_char", "true")
    mm_xsl20.addParameter("pageID2", "homePage")
    mm_xsl20.addParameter("pageID2_char", "true")    
    Response.Write(mm_xsl20.Transform())
%>
#

I am feeding breadcrumbs into a page via xml and a xslt fragment. My problem is that my xslt is looped through and the div breadcrumbtrail is repeated How do I place that in the xslt so that the div breadcrumbtrail is only shown once and all of the if items are contained in it?

Thanks

<xsl:template match="/pageInfo/page">
   <div id="breadcrumbtrail">
       <xsl:if test="name[. = $pageID1]">
          <a href="{url}"><xsl:value-of select="breadCrumbName"/></a>         
          <xsl:if test="$pageID1_char = 'true'">&#62;</xsl:if>
       </xsl:if>
       <xsl:if test="name[. = $pageID2]">
         <a href="{url}"><xsl:value-of select="breadCrumbName"/></a>
         <xsl:if test="$pageID2_char = 'true'">&#62;</xsl:if>
       </xsl:if>
       <xsl:if test="name[. = $pageID3]">
          <a href="{url}"><xsl:value-of select="breadCrumbName"/></a>
          <xsl:if test="$pageID3_char = 'true'">&#62;</xsl:if>
         </xsl:if>
       <xsl:if test="name[. = $pageID4]">
          <a href="{url}"><xsl:value-of select="breadCrumbName"/></a> 
      </xsl:if>
    </div>
</xsl:template>
+2  A: 

My problem is that my xslt is looped through and the div breadcrumbtrail is repeated How do I place that in the xslt so that the div breadcrumbtrail is only shown once and all of the if items are contained in it?

Here is what I consider a good way to do this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:meta="my:metadata" exclude-result-prefixes="meta">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <meta:pageIds>
   <page pid="p1"/>
   <page pid="p2"/>
   <page pid="p3"/>
   <page pid="p4"/>
 </meta:pageIds>

 <xsl:template match="/*">
  <div id="breadcrumbtrail">
   <xsl:apply-templates/>
  </div>
 </xsl:template>

 <xsl:template match="page[name=document('')/*/meta:pageIds/page/@pid]">
  <a href="{url}">
   <xsl:apply-templates select="breadCrumbName"/>
  </a>
  <xsl:if test=
   "document('')/*/meta:pageIds/page
                              [not(position()
                                  =
                                   last()
                                   )
                               ]
                                /@pid=current()/name">
   <xsl:text>&#62;</xsl:text>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<pageInfo>
 <page>
  <name>p1</name>
  <url>http://myUrl.com/p1/&lt;/url&gt;
  <breadCrumbName>News</breadCrumbName>
 </page>
 <page>
  <name>p2</name>
  <url>http://myUrl.com/p2/&lt;/url&gt;
  <breadCrumbName>International</breadCrumbName>
 </page>
 <page>
  <name>p3</name>
  <url>http://myUrl.com/p3/&lt;/url&gt;
  <breadCrumbName>Europe</breadCrumbName>
 </page>
 <page>
  <name>p4</name>
  <url>http://myUrl.com/p4/&lt;/url&gt;
  <breadCrumbName>France</breadCrumbName>
 </page>
</pageInfo>

the wanted, correct result is produced:

<div id="breadcrumbtrail">
    <a href="http://myUrl.com/p1/"&gt;News&lt;/a&gt;&amp;gt;
    <a href="http://myUrl.com/p2/"&gt;International&lt;/a&gt;&amp;gt;
    <a href="http://myUrl.com/p3/"&gt;Europe&lt;/a&gt;&amp;gt;
    <a href="http://myUrl.com/p4/"&gt;France&lt;/a&gt;
</div>

Do note:

  1. Most of the conditional logic in your code is unnecessary and is eliminated in this solution.

  2. The list of pages that participate in forming the breadcrumb can be easily modified/extended without requiring any changes in the code.

  3. The globally-defined element <meta:pageIds> is best put in its separate XML document. This will allow it to be edited without touching at all the XSLT code.

Dimitre Novatchev
@Dimitre: +1 faster, faster. Ja! Also, the usefull recommendation to put `<meta:pageIds>` in external document allows to compile the stylesheet without the needing to know if metadata was extended.
Alejandro