O
O
olijen2016-07-05 13:11:39
PHP
olijen, 2016-07-05 13:11:39

How to loop through a non-nested XML structure?

How in XSL to display such a set of tags one by one through a loop? Tags can be unlimited.

<main>
  <tag ROWNUM="1">Олег</tag>
  <tag ROWNUM="2">Виктор</tag>
  <tag ROWNUM="3">Денис</tag>
  <tag ROWNUM="4">Кирилл</tag>
  <tag ROWNUM="1">Олегович</tag>
  <tag ROWNUM="2">Викторович</tag>
  <tag ROWNUM="3">Денисович3</tag>
  <tag ROWNUM="4">Кириллович</tag>
</main>

So that it was "Oleg Olegovich, Viktor Viktorovich, Denis Denisovich" ...?
There used to be a nested structure and parsed through for-each. Now the sign of the group is in ROWNUM and everything is in one tag

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
olijen, 2016-07-06
@olijen

I asked myself - I answered it myself =) With stackoverflow
XSLT 1.0. Three options.
Following-sibling is used twice, which is ugly and wasteful.

<xsl:for-each select="//tag[@ROWNUM = following-sibling::tag/@ROWNUM]">
    <name>
        <xsl:value-of select="."/>
        <xsl:variable name="rownum" select="@ROWNUM"/>
        <xsl:value-of select="./following-sibling::tag[@ROWNUM = $rownum]"/>
    </name>
</xsl:for-each>

We use the key. And two cycles, which is also inelegant.
<xsl:for-each select="//tag[@ROWNUM = following-sibling::tag/@ROWNUM]/@ROWNUM">
    <name>
        <xsl:for-each select="key('num', .)">
            <xsl:value-of select="."/>
        </xsl:for-each>
    </name>
</xsl:for-each>

We use the key again. Instead of the second loop, we use index access.
<xsl:for-each select="//tag[@ROWNUM = following-sibling::tag/@ROWNUM]/@ROWNUM">
    <name>
        <xsl:value-of select="key('num', .)[1]"/>
        <xsl:value-of select="key('num', .)[2]"/>
    </name>
</xsl:for-each>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question