N
N
Nikita Melikhov2017-04-07 17:57:44
XSL & XSLT
Nikita Melikhov, 2017-04-07 17:57:44

How to select two elements in XSLT?

The bottom line is this: there is an XML file:

<items>
    <item id="1" src="/1.jpg" />
    <item id="2" src="/2.jpg" />
    <item id="3" src="/3.jpg" />
    <item id="4" src="/4.jpg" />
    <item id="5" src="/5.jpg" />
</items>

For an image slider there is a template like this:
...
    <ul>
        <xsl:apply-templates select="items/item" />
    </ul>
...
<xsl:template match="item">
    <li>
        <img src="@src" />
    </li>
</xsl:template>

I need to add two elements from XML to li so that the slider has two pictures in one block. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Fov, 2017-04-07
@VeroLom

Can it be like this

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/items">
    <ul>
      <xsl:apply-templates select="item[position() mod 2 = 1]" />
    </ul>
  </xsl:template>

  <xsl:template match="item">
    <li>
      <img src="{@src}"/>
      <img src="{following::item/@src}"/>
    </li>
  </xsl:template>
</xsl:stylesheet>

Will issue
<ul>
   <li>
      <img src="/1.jpg"/>
      <img src="/2.jpg"/>
   </li>
   <li>
      <img src="/3.jpg"/>
      <img src="/4.jpg"/>
   </li>
   <li>
      <img src="/5.jpg"/>
      <img src=""/>
   </li>
</ul>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question