'Check if image exists in xslt

My task is to check if image exist and display it. Upper node is Hotel.

Xml file:

    <PhotoList>
        <Photo>
            <Url>https://image.shutterstock.com/image-photo/hotel-word-golden-letters-
on-600w-378101848.jpg</Url>
        </Photo>
        <Photo>
            <Url>http://demotest.itravelsoftware.com/fotografije_itravel/7/715_636077306767263022.jpg</Url>
        </Photo>
        <Photo>
            <Url>http://demotest.itravelsoftware.com/fotografije_itravel/7/714_636077303419440444.jpg</Url>
        </Photo>
        <Photo>
            <Url>http://demotest.itravelsoftware.com/fotografije_itravel/7/539_636064349608545756.jpg</Url>
        </Photo>
    </PhotoList>

Xslt:

        <xsl:for-each select="PhotoList/Photo">
            <xsl:choose>
                <xsl:when test="Photo != ''">
                    <td><img src="Photo" height="100" width="100"/></td>
                </xsl:when>
            </xsl:choose>

    </xsl:for-each>

This doesn't display picture and i'm not sure if != '' could properly check if img does or doesn't exists.



Solution 1:[1]

Maybe the code you are looking for is

<xsl:for-each select="PhotoList/Photo">
    <xsl:choose>
        <xsl:when test="normalize-space(Url) != ''">
            <td><img src="{Url}" height="100" width="100"/></td>
        </xsl:when>
    </xsl:choose>
</xsl:for-each>

But it's only useful if there exist Photo elements with an empty Url child. The function normalize-space(Url) assures that Url elements with only whitespace content are handled as empty.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zx485