2008-12-03 58 views
10

Làm cách nào để lưu các lần lặp đã xảy ra trong xsl: for-each? (các biến trong XSL là không thay đổi)xsl: cho mỗi vòng lặp truy cập

Mục tiêu của tôi là tìm số lượng MAX trẻ em cho bất kỳ nút nào ở một cấp cụ thể.

Ví dụ, tôi có thể muốn in rằng không có hơn 2 nút đáp ứng cho bất kỳ câu hỏi trong cuộc khảo sát này:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="testing.xsl"?> 
<Survey> 
    <Question> 
    <Response text="Website" /> 
    <Response text="Print Ad" /> 
    </Question> 
    <Question> 
    <Response text="Yes" /> 
    </Question> 
</Survey> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="/"> 
    <html> 
    <head> 
    </head> 
    <body> 
    <xsl:for-each select="Survey"> 
     The survey has <xsl:value-of select="count(child::Question)"/> questions. 
     <br /> 
     <xsl:variable name="counter">0</xsl:variable> 
     <xsl:for-each select="Question"> 
     <!-- TODO: increment the counter ??????? --> 
     </xsl:for-each> 
     No more than <xsl:value-of select="$counter"/> responses were returned for any question. 
    </xsl:for-each> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

Trả lời

10

Một không "cứu lặp lại đã xảy ra trong một xsl: cho mỗi "vì XSLT is a functional language và các biến là không thay đổi.

Việc chuyển đổi sau tìm tối đa truy nã:

 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

    <xsl:template match="/"> 
     <xsl:call-template name="maximum"> 
     <xsl:with-param name="pNodes" select="*/Question"/> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="maximum"> 
     <xsl:param name="pNodes"/> 

     <xsl:variable name="vNumNodes" select="count($pNodes)"/> 

     <xsl:choose> 
     <xsl:when test="$vNumNodes = 1"> 
      <xsl:value-of select="count($pNodes[1]/Response)"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:variable name="vHalf" 
       select="floor($vNumNodes div 2)"/> 

      <xsl:variable name="vMax1"> 
      <xsl:call-template name="maximum"> 
      <xsl:with-param name="pNodes" 
       select="$pNodes[not(position() > $vHalf)]"/> 
      </xsl:call-template> 
      </xsl:variable> 

      <xsl:variable name="vMax2"> 
      <xsl:call-template name="maximum"> 
      <xsl:with-param name="pNodes" 
       select="$pNodes[position() > $vHalf]"/> 
      </xsl:call-template> 
      </xsl:variable> 

      <xsl:value-of select= 
      "$vMax1*($vMax1 >= $vMax2) + $vMax2*($vMax2 > $vMax1)"/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

Khi áp dụng trên tài liệu XML cung cấp:

 
<Survey> 
    <Question> 
     <Response text="Website" /> 
     <Response text="Print Ad" /> 
    </Question> 
    <Question> 
     <Response text="Yes" /> 
    </Question> 
</Survey> 

kết quả mong muốn được sản xuất:

Do lưu ý những điều sau: Mẫu tên "maximum" tự gọi mình một cách đệ quy và thực hiện các DVC (Divide and Conquer principle)để giảm thiểu đệ quy đống sâu. Danh sách các nút được chia thành hai, tối đa của hai danh sách được tính toán (đệ quy) và lớn hơn của hai được trả về.

Các vấn đề liên quan