xsl将选择结果包装在div标签中

时间:2013-11-07 19:42:46

标签: xslt xslt-1.0 xsl-choose

如何将“when”和“otherwise”包装成标签?

在我下面的例子中,我尝试添加一个div,但它最终会包装每个结果。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
   <xsl:output method="xml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" encoding="UTF-8" />
   <xsl:include href="vcardbook.xsl" />
   <xsl:template name="entriesLoopBook">
      <div id="team-posts">
         <xsl:for-each select="entries/entry ">
            <div>
               <xsl:choose>
                  <xsl:when test="$fid != 887">
                     <div class="physical">
                        <xsl:call-template name="vcardbook" />
                     </div>
                  </xsl:when>
                  <xsl:otherwise>
                     <div class="service">
                        <xsl:call-template name="vcardbook" />
                     </div>
                  </xsl:otherwise>
               </xsl:choose>
            </div>
         </xsl:for-each>
      </div>
      <div style="clear:both;" />
   </xsl:template>
</xsl:stylesheet>

上述代码的结果:

<div>
   <div class="physical"><div class="567"></div></div>
   <div class="physical"><div class="457"></div></div>
   <div class="physical"><div class="342"></div></div>
   <div  class="service"><div class="887"></div></div>
   <div  class="service"><div class="887"></div></div>
</div>

我希望它看起来如何:

<div>
   <div class="physical">
      <div class="567"></div>
      <div class="457"></div>
      <div class="342"></div>
   </div>
   <div  class="service">
     <div class="887"></div>
     <div class="887"></div>
   </div>
</div>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

根据我的规定,我不建议您使用for-each,但要对代码进行尽可能少的更改,请尝试以下操作:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" version="1.0">
   <xsl:output method="xml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" encoding="UTF-8" />
   <xsl:include href="vcardbook.xsl" />
   <xsl:template name="entriesLoopBook">
      <div id="team-posts">
         <div>
            <div class="physical">
              <xsl:for-each select="entries/entry[<wherever you get $fid from> != 887">
                <xsl:call-template name="vcardbook"/>
              </xsl:for-each>
            </div>
            <div class="service">
              <xsl:for-each select="entries/entry[<wherever you get $fid from> = 887">
                <xsl:call-template name="vcardbook"/>
              </xsl:for-each>
            </div>
         </dv>
      </div>
      <div style="clear:both;" />
   </xsl:template>
</xsl:stylesheet>