I have XSL:

<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<body>
<table>
<tr>
<td><b>Song</b></td>
<td><b>Track</b></td>
</tr>
<xsl:for-each select="results/result[Artist='Michael Jackson']">
<tr>
<td><xsl:value-of select="Song"/></td>
<td><xsl:value-of select="Track"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>

And source XML

<?xml version="1.0"?>
<results>
    <result>
        <Artist>
            Michael Jackson
        </Artist>
        <Song>
            Beat it 
        </Song>
        <Track>
            2 
        </Track>
    </result>
</results>

The result yields no rows, where I expect to see a table with results, where Michael Jackson is the artist.

I have done some testing and I think the indent, or empty space in the source is getting in the way of the select.

When I use source...

<?xml version="1.0"?>
<results>
<result>
<Artist>Michael Jackson</Artist>
<Song>Beat it </Song>
<Track>2 </Track>
</result>
</results>

It works.

Unfortunately I have no control of the source format. I feel like there must be a simple answer as a lot of XML is indented this way.

Is there a trim function or wildcard I can use to make the [] select/filter work?

Many thanks

有帮助吗?

解决方案

Use the normalize-space() function:

<xsl:for-each select="results/result[normalize-space(Artist)='Michael Jackson']">
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top