I'm trying to teach myself XSL and XPATH. I have a sample XML document created by one of our commercial tools, and I want to extract certain node values and create a CSV file as output. A truncated example of the XML document is here:
<?xml version="1.0" encoding="windows-1252"?>
<xml_report>
<form id= "WOI:WorkOrder" xmlns="http://www.w3.org/2000/xforms">
<mode l>
< group name="field-info" minOccurs="1" maxOccurs="1">
<group name="field" minOccurs="1" maxOccurs="*">
<string name="name" />
<number name="id" long="true" />
<string name="type" range="closed">
<value>CHAR</value>
<value>TIME</value>
<value>DECIMAL</value>
<value>REAL</value>
<value>INT</value>
<value>ENUM</value>
<value>ATTACH</value>
<value>DIARY</value>
<value>TIMEOFDAY</value>
<value>DATE</value>
<value>CURRENCY</value>
<value>NULL</value>
</string>
</group>
<!-- Additional group nodes -->
</group>
</model>
<instance>
<field-info>
<field>
<name>Work Order ID*+</name>
<id>1000000182</id>
<type> CHAR</type>
</field>
<!-- Additional field nodes -->
</field-info>
<entry>
<field_value>
<value>WO0000000498983</value>
</field_value>
<field_value>
<value>New Host name for new server build</value>
</field_value>
</entry>
<!-- Additional entry nodes -->
</instance>
</form>
</xml_report>
I want to extract the contents of the value elements only, filtering out everything else. I've written some pretty unsophisticated XSL to attempt to do this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" indent="yes" encoding="utf-8" media-type="text/plain" />
<xsl:template match="/xml_report/form/instance">
<xsl:for-each select="entry/field_value">
<xsl:value-of select='value' /><xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Given the example XML, I would expect the following output:
WO0000000498983,New Host name for new server build,
The issue is that I'm actually extracting the value of ALL elements preceding the node list I actually want to work with, as well as unwanted indents and line spacing. I thought that specifying a restrictive XPATH expression in the template match and for-each tags would suffice, but it does not. How can I narrow the range of selected nodes to only those that I actually want to use? I'm using SAXON as the XSLT processing engine on Windows 7 if that helps.
CHAR
TIME
DECIMAL
REAL
INT
ENUM
ATTACH
DIARY
TIMEOFDAY
DATE
CURRENCY
NULL
Work Order ID*+
1000000182
CHAR
WO0000000498983
New Host name for new server build