2

I have a question about Xslt mod. I need to change the div class of every 3rd div from "clmn-3 doublespace" to "clmn-3". Tried several tips but could not understand where to add the argument.

    <div class="clmn-9 products">
    <h1>Title</h1>
    <xsl:if test="$mode = 'edit'">
    <xsl:text disable-output-escaping="yes"><![CDATA[<div class="webtop">]]></xsl:text>
    </xsl:if>
    <xsl:call-template name="ndSitePageList">
    <xsl:with-param name="sPgLstID" select="1" />
    <xsl:with-param name="customTemplate">
    <xsl:for-each select="/ndTemplate/ndSitePageLists/ndList[instanceID=1]/ndSitePage">
    <xsl:call-template name="ndReplaceListSitePageElements">
    <xsl:with-param name="template">
    <xsl:text disable-output-escaping="yes">
    <![CDATA[
    <div class="clmn-3 doublespace"> 
    <a href="]]></xsl:text><xsl:value-of select="link"/><xsl:text disable-output-escaping="no">  <![CDATA["><img src="]]></xsl:text><xsl:value-of select="/ndTemplate/ndSpgPath"/><xsl:value-of select="srcThumb"/><xsl:text disable-output-escaping="no"><![CDATA["alt="" /></a>
    <div class="clmn-3 shadow"></div>
    <h2>]]></xsl:text><xsl:value-of select="title"/><xsl:text disable-output-escaping="no"><![CDATA[</h2>
    <span class="text">]]></xsl:text><xsl:value-of select="description"/><xsl:text disable-output-escaping="no"><![CDATA[</span>
    </div>
    ]]>
    </xsl:text>
    </xsl:with-param>
    </xsl:call-template>
    </xsl:for-each>
    </xsl:with-param>
    </xsl:call-template>
    </div>

Thank you in advance Alex

4

1 回答 1

1

I. 下面是如何在 XSLT 1.0 中指定此类处理的示例:

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

 <xsl:param name="pNewValue" select="'someNewAttribValue'"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="num[position() mod 3 = 1]">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="class"><xsl:value-of select="$pNewValue"/></xsl:attribute>
    <xsl:apply-templates select="node()"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时(问题中未提供任何内容!):

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

产生了想要的正确结果(每三个num元素都有一个class属性,其值在外部参数中指定)

<nums>
   <num class="someNewAttribValue">01</num>
   <num>02</num>
   <num>03</num>
   <num class="someNewAttribValue">04</num>
   <num>05</num>
   <num>06</num>
   <num class="someNewAttribValue">07</num>
   <num>08</num>
   <num>09</num>
   <num class="someNewAttribValue">10</num>
</nums>

二、XSLT 2.0 解决方案

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:param name="pNewValue" select="'someNewAttribValue'"/>

 <xsl:template match="/*">
     <nums>
       <xsl:for-each-group select="*" group-adjacent="position() mod 3 eq 1">
         <xsl:sequence select="current-group()[not(current-grouping-key())]"/>
         <xsl:apply-templates select="current-group()[current-grouping-key()]"/>
       </xsl:for-each-group>
     </nums>
 </xsl:template>

 <xsl:template match="num">
  <num class="{$pNewValue}"/>
 </xsl:template>
</xsl:stylesheet>

当这个 XSLT 2.0 转换应用于同一个 XML 文档(上图)时,会产生同样想要的正确结果

<nums>
   <num class="someNewAttribValue"/>
   <num>02</num>
   <num>03</num>
   <num class="someNewAttribValue"/>
   <num>05</num>
   <num>06</num>
   <num class="someNewAttribValue"/>
   <num>08</num>
   <num>09</num>
   <num class="someNewAttribValue"/>
</nums>
于 2012-07-05T03:17:06.850 回答