xslt return selection of following siblings

2019-08-11 07:00发布

问题:

I have a selection of XML (edited)

<items>                 
    <item id="00100537" label="A/137i r" lcn="005417713" notes="A/137-160"/>
    <item id="00100538" label="A/137i v" lcn="" notes=""/>
    <item id="00100539" label="A/137ii r" lcn="" notes=""/>
    <item id="00100540" label="A/137ii v" lcn="" notes=""/>
    <item id="00100678" label="A/159iii v" lcn="" notes=""/>
    <item id="00100679" label="A/159iv r" lcn="" notes=""/>
    <item id="00100680" label="A/159iv v" lcn="" notes=""/>
    <item id="00100681" label="A/160i r" lcn="" notes=""/>
    <item id="00100682" label="A/160i v" lcn="" notes=""/>
    <item id="00100683" label="A/160ii r" lcn="" notes=""/>
    <item id="00100684" label="A/160ii v" lcn="" notes=""/>
    <item id="00100685" label="A/160iii r" lcn="" notes=""/>
    <item id="00100686" label="A/160iii v" lcn="" notes=""/>
    <item id="00100687" label="A/161i r" lcn="005417714" notes="A/161-182"/>
    <item id="00100688" label="A/161i v" lcn="" notes=""/>
    <item id="00100819" label="A/182ii v" lcn="" notes=""/>
    <item id="00100820" label="A/182iii r" lcn="" notes=""/>
    <item id="00100821" label="A/182iii v" lcn="" notes=""/>
    <item id="00100822" label="A/183i r" lcn="005417715" notes="A/183-218"/>
    <item id="00100823" label="A/183i v" lcn="" notes=""/>
    <item id="00100975" label="A/216iii r" lcn="" notes=""/>
    <item id="00100976" label="A/216iii v" lcn="" notes=""/>
    <item id="00100977" label="A/217i r" lcn="" notes=""/>
    <item id="00100978" label="A/217i v" lcn="" notes=""/>
    <item id="00100979" label="A/217ii r" lcn="" notes=""/>
    <item id="00100980" label="A/217ii v" lcn="" notes=""/>
    <item id="00100981" label="A/218i r" lcn="" notes=""/>
    <item id="00100982" label="A/218i v" lcn="" notes=""/>
    <item id="00100983" label="A/218ii r" lcn="" notes=""/>
    <item id="00100984" label="A/218ii v" lcn="" notes=""/>
    <item id="00100985" label="A/219i r" lcn="005417716" notes="A/219-248"/>
    <item id="00100986" label="A/219 v" lcn="" notes=""/>
    <item id="00100987" label="A/219ii r" lcn="" notes=""/>
    <item id="00101061" label="A/248 r" lcn="" notes=""/>
    <item id="00101062" label="A/248 v" lcn="" notes=""/>
</items>

I want to be able to get all following nodes from a given lcn before the next lcn.

If I start at lcn='005417714' I know there are 4 following nodes.

But when I try and do something like this

<xsl:for-each select="/items/item[@lcn='005417714']/following-sibling::*">

I get all following-siblings. How can I get all following-siblings before the next non-empty lcn attribute? So retrieve only the 4 following siblings? thanks

回答1:

In XSLT 2.0 you could use <xsl:for-each-group> and in particular its group-starting-with attribute to iterate over groups of elements starting with a non-empty lcn. In XSLT 1.0 there are various possibilities. The simplest approach would be something like

<xsl:for-each select="/items/item[@lcn='005417714']">
  <xsl:for-each select="following-sibling::*[
         generate-id(preceding-sibling::item[@lcn != ''][1])
       = generate-id(current())]">
  </xsl:for-each>
</xsl:for-each>

which examines all the following siblings of our target item and selects those whose nearest preceding-sibling with a non-empty lcn attribute is the target item.

If you want to iterate over all the "sections" rather than just targetting one specific one then the key-based grouping approach suggested by hr_117 would be more efficient.



回答2:

With xslt-1.0 you may try something like this key based solution: (Only to show how it could work)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:key name="kfItem" match="item[@lcn = '']"
         use="generate-id(preceding-sibling::item[@lcn != ''][1])"/>

    <xsl:template match="/*">
        <items>
            <xsl:apply-templates select="item[@lcn != '']"/>
        </items>
    </xsl:template>

    <xsl:template match="item">
        <lcn lcn="{@lcn}">
            <xsl:copy-of select="."/>
            <xsl:copy-of select="key('kfItem', generate-id())"/>
        </lcn>
    </xsl:template>
</xsl:stylesheet>

Which will generate the following output:

<items>
  <lcn lcn="005417713">
    <item id="00100537" label="A/137i r" lcn="005417713" notes="A/137-160"/>
    <item id="00100538" label="A/137i v" lcn="" notes=""/>
    <item id="00100539" label="A/137ii r" lcn="" notes=""/>
    <item id="00100540" label="A/137ii v" lcn="" notes=""/>
    <item id="00100678" label="A/159iii v" lcn="" notes=""/>
    <item id="00100679" label="A/159iv r" lcn="" notes=""/>
    <item id="00100680" label="A/159iv v" lcn="" notes=""/>
    <item id="00100681" label="A/160i r" lcn="" notes=""/>
    <item id="00100682" label="A/160i v" lcn="" notes=""/>
    <item id="00100683" label="A/160ii r" lcn="" notes=""/>
    <item id="00100684" label="A/160ii v" lcn="" notes=""/>
    <item id="00100685" label="A/160iii r" lcn="" notes=""/>
    <item id="00100686" label="A/160iii v" lcn="" notes=""/>
  </lcn>
  <lcn lcn="005417714">
    <item id="00100687" label="A/161i r" lcn="005417714" notes="A/161-182"/>
    <item id="00100688" label="A/161i v" lcn="" notes=""/>
    <item id="00100819" label="A/182ii v" lcn="" notes=""/>
    <item id="00100820" label="A/182iii r" lcn="" notes=""/>
    <item id="00100821" label="A/182iii v" lcn="" notes=""/>
  </lcn>
  <lcn lcn="005417715">
    <item id="00100822" label="A/183i r" lcn="005417715" notes="A/183-218"/>
    <item id="00100823" label="A/183i v" lcn="" notes=""/>
    <item id="00100975" label="A/216iii r" lcn="" notes=""/>
    <item id="00100976" label="A/216iii v" lcn="" notes=""/>
    <item id="00100977" label="A/217i r" lcn="" notes=""/>
    <item id="00100978" label="A/217i v" lcn="" notes=""/>
    <item id="00100979" label="A/217ii r" lcn="" notes=""/>
    <item id="00100980" label="A/217ii v" lcn="" notes=""/>
    <item id="00100981" label="A/218i r" lcn="" notes=""/>
    <item id="00100982" label="A/218i v" lcn="" notes=""/>
    <item id="00100983" label="A/218ii r" lcn="" notes=""/>
    <item id="00100984" label="A/218ii v" lcn="" notes=""/>
  </lcn>
  <lcn lcn="005417716">
    <item id="00100985" label="A/219i r" lcn="005417716" notes="A/219-248"/>
    <item id="00100986" label="A/219 v" lcn="" notes=""/>
    <item id="00100987" label="A/219ii r" lcn="" notes=""/>
    <item id="00101061" label="A/248 r" lcn="" notes=""/>
    <item id="00101062" label="A/248 v" lcn="" notes=""/>
  </lcn>
</items>


标签: xslt