How to get a proceeding item that may be in another node?

Question

With the sample XML:

<orders>
    <order>
        <items>
            <item>1</item>
            <item>2</item>
        </items>
    </order>
    <order>
        <items>
            <item>3</item>
            <item>4</item>
        </items>
    </order>
</orders>

I am currently rendering out a spreadsheet that essentially makes a top level. I'm doing this through an apply-templates, where I have

<apply-templates match="orders/order/items/item"><!-- sorts --></apply-templates>

I want to apply a header whenever a certain attribute in the item changes, how would I get the previous item?

for item4, I think I could use preceding-sibling::item to get item3, but if my active node was item3, how would I get item2?

I'm currently assuming I'll need to set this to a variable with a choose, something like:

<xslt:variable name="previousItem">
    <xslt:choose>
        <xslt:when test="count(preceding-sibling::item)">
            <xslt:value-of match="preceding-sibling::item" />
        </xslt:when>
        <xslt:otherwise>
            <!-- some logic here -->
        </xslt:otherwise>
    </xslt:choose>
</xslt:variable>

Show source
| xml   | xslt   2016-12-12 20:12 1 Answers

Answers to How to get a proceeding item that may be in another node? ( 1 )

  1. 2016-12-12 21:12

    Try preceding::item[1]. It only selects items that occurs before the current element.

Leave a reply to - How to get a proceeding item that may be in another node?

◀ Go back