XSLT: تمرير قيمة من واحد لكل مباراة إلى التالي

StackOverflow https://stackoverflow.com/questions/1834385

  •  11-09-2019
  •  | 
  •  

سؤال

أنا أستخدم ما يلي لمطابقة الكل <section>S مع مجموعة سمة المراجعة. <section>يمكن أن تظهر في العديد من المستويات المختلفة لشجرة المستندات، تحتوي دائما على <chapter>س.

<xsl:for-each select="//section[@revision]">
    <!-- Do one thing if this is the first section 
           matched in this chapter -->

    <!-- Do something else if this section is in the same 
           chapter as the last section matched -->
</xsl:for-each>

كما تقول التعليقات، أحتاج إلى جعل كل منها for-each تدرك التكرار الفصل الذي السابق القسم المتطابق ينتمي. وأنا أعلم ذلك <xsl:variable>S هي في الواقع ثابتة بمجرد تعيين، وهذا <xsl:param> ينطبق فقط على استدعاء قوالب.

هذا كونه docbook، يمكنني إعادة التقاط رقم الفصل القسم مع:

<xsl:apply-templates select="ancestor::chapter[1]" mode="label.markup" />

لكنني أعتقد أنه يمكن القيام به مع XPath بحتة.

أيه أفكار؟ شكرا!

هل كانت مفيدة؟

المحلول

لست متأكدا مما إذا كنت غير جولة متطلباتك 100٪، ولكن ...

<xsl:variable name="sections" select="//section[@revision]" />

<xsl:for-each select="$sections">
  <xsl:variable name="ThisPos" select="position()" />
  <xsl:variable name="PrevMatchedSection" select="$sections[$ThisPos - 1]" />
  <xsl:choose>
    <xsl:when test="
      not($PrevMatchedSection)
      or
      generate-id($PrevMatchedSection/ancestor::chapter[1])
      !=
      generate-id(ancestor::chapter[1])
    ">
      <!-- Do one thing if this is the first section 
           matched in this chapter -->
    </xsl:when>
    <xsl:otherwise>
      <!-- Do something else if this section is in the same 
           chapter as the last section matched -->
    </xsl:otherwise>
  </xsl:choose>  
</xsl:for-each>

ومع ذلك، أظن أن هذا الشيء كله يمكن حله بشكل أكبر مع <xsl:template> / <xsl:apply-templates> نهج. ولكن دون رؤية المدخلات الخاصة بك والمخرج المتوقع من الصعب القول.

نصائح أخرى

سيؤدي الموضع () إلى عودة موقعك داخل الحالي لكل تكرار. سيعود التكرار الأول، IIRC، 0، لذا فإن اختبار "الموقف () = 0" سيخبرك إذا كنت في التكرار الأول.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top