Reputation: 33
I'm rather new to the world of xml and xslt. What I am wanting to do is use an xslt to return all the error messages generated in an xml file, there are many error messages under each parent.
Here's an example of the XML file:
<progress_file>
<read_leg>
<info>Successfully read face ID 225</info>
<info>successfully read face ID 226</info>
<error>unable to read face ID 227</error>
<error>unable to read face ID 228</error>
</read_leg>
<write_leg>
<info>Successfully created face ID 225</info>
<info>successfully created face ID 226</info>
<error>unable to write face ID 227</error>
<error>unable to write face ID 228</error>
</write_leg>
</progress_file>
The XSLT used is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="progress_file/read_leg">
<xsl:value-of select="error"/>
</xsl:for-each>
<xsl:for-each select="progress_file/write_leg">
<xsl:value-of select="error"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The output only returns the first value from each area. I gather that this is what the logic implies, i.e. "for each write leg, return the error message" and this doesn't mean it checks if there are multiple cases.
I haven't seen anywhere that has multiple attributes with the same name and I haven't come across and XSL element that can work with this, so I'm a bit stuck. Any suggestions on how this is possible?
One further question, is it possible to get line breaks in between the output lines?
Thanks.
Upvotes: 2
Views: 2429
Reputation: 2585
Here's one option:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="utf-8"/>
<!-- The XML entity for a line feed -->
<xsl:variable name="linefeed" select="' '"/>
<!-- Match the root node -->
<xsl:template match="/">
<!-- Apply templates for all <error> nodes. -->
<xsl:apply-templates select="progress_file/read_leg/error | progress_file/write_leg/error"/>
</xsl:template>
<xsl:template match="error">
<!-- Concatenate the value of the current node and a line feed. -->
<xsl:value-of select="concat(., $linefeed)"/>
</xsl:template>
</xsl:stylesheet>
unable to read face ID 227
unable to read face ID 228
unable to write face ID 227
unable to write face ID 228
Upvotes: 3