在if中声明变量,并在下一个if中使用它

时间:2013-03-21 17:50:23

标签: xslt umbraco xslt-1.0

我在工作中遇到了一个问题,我目前正在开发一个已经建成的网站(我自己没有开发),而且我对Umbraco和xslt并不熟悉..

这是我正在处理的代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:msxml="urn:schemas-microsoft-com:xslt"
   xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
   exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

   <xsl:output method="xml" omit-xml-declaration="yes"/>

   <xsl:template match="/">
   <style>
      span.cc { font-size:9px;border:1px solid #000;margin-right:3px; }
   </style>
   <xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
   <xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
   <table width="100%">

   <xsl:variable name="loc" select="umbraco.library:GetXmlDocument('/media/31087/data.xml', 1)"/>
   <xsl:for-each select="$loc/rdsppGenData/generatorData">
     <xsl:variable name="tags">
       <xsl:if test="(OA23) and (not(OA23 = '-'))">cat_1223 </xsl:if>
       <xsl:if test="(OA30) and (not(OA30 = '-'))">cat_1868 </xsl:if>
       <xsl:if test="(OA36) and (not(OA36 = '-'))">cat_1227 </xsl:if>
       <xsl:if test="(OA60) and (not(OA60 = '-'))">cat_1881 </xsl:if>
       <xsl:if test="(F0) and (F0 = 'Z01')">cat_1229 </xsl:if>
       <xsl:if test="(F0) and (F0 = 'X01')">cat_1802 </xsl:if>
     </xsl:variable>
     <xsl:variable name="system">
       <xsl:choose>
         <xsl:when test="F0">
           <xsl:value-of select="F1"/>
         </xsl:when>
         <xsl:when test="(OA23) or (OA30) or (OA36) or (OA60)">
           <xsl:value-of select="system"/>
         </xsl:when>
       </xsl:choose>
     </xsl:variable>
     <xsl:variable name="TE">
       <xsl:choose>
         <xsl:when test="F0">
           <xsl:value-of select="F2"/>
         </xsl:when>
         <xsl:when test="(OA23) or (OA30) or (OA36) or (OA60)">
           <xsl:value-of select="TE"/>
         </xsl:when>
       </xsl:choose>
     </xsl:variable>
     <xsl:variable name="P1">
       <xsl:choose>
         <xsl:when test="F0">
           <xsl:value-of select="P1s"/>
         </xsl:when>
         <xsl:when test="(OA23) or (OA30) or (OA36) or (OA60)">
           <xsl:value-of select="P1"/>
         </xsl:when>
       </xsl:choose>
     </xsl:variable>
     <xsl:variable name="P2">
       <xsl:choose>
         <xsl:when test="F0">
           <xsl:value-of select="P2s"/>
         </xsl:when>
         <xsl:when test="(OA23) or (OA30) or (OA36) or (OA60)">
           <xsl:value-of select="P2"/>
         </xsl:when>
       </xsl:choose>
     </xsl:variable>
     <xsl:variable name="description">
       <xsl:choose>
         <xsl:when test="F0">
           <xsl:value-of select="Sdescription"/>
         </xsl:when>
         <xsl:when test="(OA23) or (OA30) or (OA36) or (OA60)">
           <xsl:value-of select="description"/>
         </xsl:when>
       </xsl:choose>
     </xsl:variable>

     <xsl:if test="not(contains($system,'_'))">
       <xsl:if test="3 = string-length($system)">
         <tr class="sys ovs_{generate-id(msxml:node-set($system))} {$tags}" id="sys_{generate-id(msxml:node-set($system))}"  >
           <td onclick="sys('{generate-id(msxml:node-set($system))}',1)" class="label">
                <xsl:value-of select="$system"/>
           </td>

          <xsl:variable name="systemname" select="translate($system,$ucletters,$lcletters)" />

          <td onclick="sys('{generate-id(msxml:node-set($system))}',1)" class="color">
              <xsl:if test="(OA23) and (not(OA23 = '-'))">
                <span class="cc" style="width:8px;background-color:#4DAA5B;">&nbsp;&nbsp;</span>
              </xsl:if>
              <xsl:if test="(OA30) and (not(OA30 = '-'))">
                <span class="cc" style="width:8px;background-color:#EDEA87;">&nbsp;&nbsp;</span>
              </xsl:if>
              <xsl:if test="(OA36) and (not(OA36 = '-'))">
                <span class="cc" style="width:8px;background-color:#EA6A00;">&nbsp;&nbsp;</span>
              </xsl:if>
              <xsl:if test="(OA60) and (not(OA60 = '-'))">
                <span class="cc" style="width:8px;background-color:#C897CC;">&nbsp;&nbsp;</span>
              </xsl:if>
              <xsl:if test="(F0) and (F0 = 'Z01')">
                <span class="cc" style="width:8px;background-color:#829ED8;">&nbsp;&nbsp;</span>
              </xsl:if>
              <xsl:if test="(F0) and (F0 = 'X01')">
                <span class="cc" style="width:8px;background-color:#9FAD00;">&nbsp;&nbsp;</span>
              </xsl:if>
          </td>
          <td onclick="sys('{generate-id(msxml:node-set($system))}',1)" class="kw">
                <span class="keyword"><xsl:value-of select="$description"/>&nbsp;</span>
          </td>
        </tr>

      </xsl:if>

      <xsl:if test="5 = string-length($system)">
        <xsl:if test="(3 > string-length($TE)) or (not($TE))">

          <xsl:variable name="tags2">
            <xsl:if test="(OA23) and (not(OA23 = '-'))">cat_1223 </xsl:if>
            <xsl:if test="(OA30) and (not(OA30 = '-'))">cat_1868 </xsl:if>
            <xsl:if test="(OA36) and (not(OA36 = '-'))">cat_1227 </xsl:if>
            <xsl:if test="(OA60) and (not(OA60 = '-'))">cat_1881 </xsl:if>
            <xsl:if test="(F0) and (F0 = 'Z01')">cat_1229 </xsl:if>
            <xsl:if test="(F0) and (F0 = 'X01')">cat_1802 </xsl:if>
          </xsl:variable>
          <xsl:variable name="second_level_id" select="generate-id(msxml:node-set($system))" />
          <tr id="sys_{generate-id(msxml:node-set($system))}" style="display:none" class="sys2 {generate-id(msxml:node-set($system))} {$tags2}">

              <td style="padding-left:30px; width:190px;" onclick="sys('{generate-id(msxml:node-set($system))}',2)" class="label">
                  <xsl:value-of select="$system"/>
              </td>
             <td class="gallery_icon">
                &nbsp;
              </td>
              <td onclick="sys('{generate-id(msxml:node-set($system))}',2)">&nbsp;</td>
              <td class="keyword" onclick="sys('{generate-id(msxml:node-set($system))}',2)">
                <xsl:value-of select="$description"/>
              </td>
          </tr>
        </xsl:if>
      </xsl:if>
      <xsl:if test="($TE) and (not($TE = '-')) and (not($TE = ''))">
        <xsl:variable name="tags3">
          <xsl:if test="(OA23) and (not(OA23 = '-'))">cat_1223 </xsl:if>
          <xsl:if test="(OA30) and (not(OA30 = '-'))">cat_1868 </xsl:if>
          <xsl:if test="(OA36) and (not(OA36 = '-'))">cat_1227 </xsl:if>
          <xsl:if test="(OA60) and (not(OA60 = '-'))">cat_1881 </xsl:if>
          <xsl:if test="(F0) and (F0 = 'Z01')">cat_1229 </xsl:if>
          <xsl:if test="(F0) and (F0 = 'X01')">cat_1802 </xsl:if>
        </xsl:variable>
        <xsl:variable name="third_level_id" select="generate-id(msxml:node-set($TE))" />
        <tr id="sys_{generate-id(msxml:node-set($TE))}"  style="display:none" class="sys3 sub_{second_level_id} {generate-id(msxml:node-set($TE))} {$tags3}">

            <td style="padding-left:60px; width:160px;" onclick="sys('{generate-id(msxml:node-set($TE))}',3)" class="label">
                <xsl:value-of select="$TE"/>
            </td>
          <td class="gallery_icon">
              &nbsp;
            </td>
            <td onclick="sys('{generate-id(msxml:node-set($TE))}',3)">&nbsp;</td>
            <td class="keyword" onclick="sys('{generate-id(msxml:node-set($TE))}',3)">
              <xsl:value-of select="$description"/>
            </td>
        </tr>
      </xsl:if>
      <xsl:if test="($P1) and (not($P1 = '-')) and (not($P1 = ''))">       
        <xsl:variable name="tags4">
          <xsl:if test="(OA23) and (not(OA23 = '-'))">cat_1223 </xsl:if>
          <xsl:if test="(OA30) and (not(OA30 = '-'))">cat_1868 </xsl:if>
          <xsl:if test="(OA36) and (not(OA36 = '-'))">cat_1227 </xsl:if>
          <xsl:if test="(OA60) and (not(OA60 = '-'))">cat_1881 </xsl:if>
          <xsl:if test="(F0) and (F0 = 'Z01')">cat_1229 </xsl:if>
          <xsl:if test="(F0) and (F0 = 'X01')">cat_1802 </xsl:if>
        </xsl:variable>
        <xsl:variable name="fourth_level_id" select="generate-id(msxml:node-set($P1))" />
        <tr id="sys_{generate-id(msxml:node-set($P1))}"  style="display:none" class="sys4 sub_{third_level_id} subi_{second_level_id} {generate-id(msxml:node-set($P1))} {$tags4}">

            <td style="padding-left:90px; width:130px;" onclick="sys('{generate-id(msxml:node-set($P1))}',4)" class="label">
                <xsl:value-of select="$P1"/>
            </td>
          <td class="gallery_icon">
              &nbsp;
            </td>
            <td onclick="sys('{generate-id(msxml:node-set($P1))}',4)">&nbsp;</td>
            <td class="keyword" onclick="sys('{generate-id(msxml:node-set($P1))}',4)">
              <xsl:value-of select="$description"/>
            </td>
        </tr>
      </xsl:if>
      <xsl:if test="($P2) and (not($P2 = '-')) and (not($P2 = ''))">
        <xsl:variable name="tags5">
          <xsl:if test="(OA23) and (not(OA23 = '-'))">cat_1223 </xsl:if>
          <xsl:if test="(OA30) and (not(OA30 = '-'))">cat_1868 </xsl:if>
          <xsl:if test="(OA36) and (not(OA36 = '-'))">cat_1227 </xsl:if>
          <xsl:if test="(OA60) and (not(OA60 = '-'))">cat_1881 </xsl:if>
          <xsl:if test="(F0) and (F0 = 'Z01')">cat_1229 </xsl:if>
          <xsl:if test="(F0) and (F0 = 'X01')">cat_1802 </xsl:if>
        </xsl:variable>
        <tr id="sys_{generate-id(msxml:node-set($P2))}"  style="display:none" class="sys5 subi_{third_level_id} subi_{second_level_id} sub_{fourth_level_id} {generate-id(msxml:node-set($P2))} {$tags5}">

            <td style="padding-left:120px; width:100px;" onclick="sys('{generate-id(msxml:node-set($P2))}',5)" class="label">
                <xsl:value-of select="$P2"/>
            </td>
          <td class="gallery_icon">
              &nbsp;
            </td>
            <td onclick="sys('{generate-id(msxml:node-set($P2))}',5)">&nbsp;</td>
            <td class="keyword" onclick="sys('{generate-id(msxml:node-set($P2))}',5)">
              <xsl:value-of select="$description"/>
            </td>
        </tr>
      </xsl:if>
    </xsl:if>
  </xsl:for-each>
 </table>
 </xsl:template>
 </xsl:stylesheet>

我正在尝试从prev获取唯一ID。 tr并在当前行中使用它..

我想使用变量:

<xsl:variable name="second_level_id" select="generate-id(msxml:node-set($system))" />

在这一行:

<tr id="sys_{generate-id(msxml:node-set($TE))}"  style="display:none" class="sys3 sub_{second_level_id} {generate-id(msxml:node-set($TE))} {$tags3}">

等等。

如果有更好的方法可以做到这一切,我会得到完全相同的输出,但是从上面得到所需的变量,我很乐意听到它。

  • 的Mathias

1 个答案:

答案 0 :(得分:1)

以下是您应该能够实现此目标的方法:

替换它:

<xsl:for-each select="$loc/rdsppGenData/generatorData">

使用:

<xsl:variable name="locItems" select="$loc/rdsppGenData/generatorData" />
<xsl:apply-templates select="$locItems[1]">
  <xsl:with-param name="remainder" select="$locItems[position() > 1" />
</xsl:apply-templates>

将此添加到您的XSLT:

<xsl:template match="generatorData">
  <xsl:param name="remainder" />
  <xsl:param name="last_second_level_id" />
  <xsl:param name="last_third_level_id" />

  <!-- Move the contents of the xsl:for-each in here -->

  <xsl:apply-templates select="$remainder[1]">
    <xsl:with-param name="remainder" select="$remainder[position() > 1]" />
    <xsl:with-param name="last_second_level_id" select="$second_level_id" />
    <xsl:with-param name="last_thirdlevel_id" select="$second_level_id" />
  </xsl:apply-templates>
</xsl:template>