开发者

XSLT and repeating div question

开发者 https://www.devze.com 2023-01-12 08:04 出处:网络
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 fragmen

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_x开发者_运维问答sl20.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>


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/</url>
  <breadCrumbName>News</breadCrumbName>
 </page>
 <page>
  <name>p2</name>
  <url>http://myUrl.com/p2/</url>
  <breadCrumbName>International</breadCrumbName>
 </page>
 <page>
  <name>p3</name>
  <url>http://myUrl.com/p3/</url>
  <breadCrumbName>Europe</breadCrumbName>
 </page>
 <page>
  <name>p4</name>
  <url>http://myUrl.com/p4/</url>
  <breadCrumbName>France</breadCrumbName>
 </page>
</pageInfo>

the wanted, correct result is produced:

<div id="breadcrumbtrail">
    <a href="http://myUrl.com/p1/">News</a>&gt;
    <a href="http://myUrl.com/p2/">International</a>&gt;
    <a href="http://myUrl.com/p3/">Europe</a>&gt;
    <a href="http://myUrl.com/p4/">France</a>
</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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号