Hi all

I need to detect all image tags in a HTML document that are siblings:

<p>
  <img ../>
  <img ../>
  <img ../>
</p>
<p>
  <img ../>
</p>

In the example above I need to detect the first three img tags, but not the fourth.

In contrary, I also need to detect the img tags that do NOT have direct img siblings.

Sadly I have no idea how to do this. Any hint, please? Thanks.
Josh

Recommended Answers

All 3 Replies

var siblings=[]
for( var i=0, limit=document.images.length-1; ++i)
{
 if( document.images[i].parentNode===document.images[i+1].parentNode)
 siblings[siblings.length]=i;
}
alert( siblings.join("\n") );

sorry, the for clause should be

for( var i=0, limit=document.images.length-1; i < limit; ++i)
...

Let's make an example that is similar to you

<abc>
    <def>
        <img>1</img>
        <img>2</img>
        <img>3</img>
    </def>
    <ghi>
        <img>4</img>
    </ghi>
    <jkl/>
</abc>

Now my code will be

<xsl:template match="/">
        <xsl:for-each select="//img[preceding-sibling::img or following-sibling::img]">
            <xsl:value-of select="."></xsl:value-of>
        </xsl:for-each>
    </xsl:template>

Hope it will be okay.

Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.