How to handle duplicate node names when converting xml to csv using java and xsl -
i given xml file outside source (so have no control on attribute names) , unfortunately use same name paired set of data. can't seem figure out how access second value. example of data in xml file is:
<?xml version="1.0"?> <addressresponse> <results> <ownername>name1</ownername> <houseaddress>house1</houseaddress> <houseaddress>citystate1</houseaddress> <yearbuilt>year1</yearbuilt> </results> <results> <ownername>name2</ownername> <houseaddress>house2</houseaddress> <houseaddress>citystate2</houseaddress> <yearbuilt>year2</yearbuilt> </results> </addressresponse>
i have java code , can parse xml need handling duplicate attribute name. want csv file following:
owner,address,citystate,yearbuilt name1,house1,citystate1,year1 name2,house2,citystate2,year2
in xsl file, did following "hoping" second houseaddress didn't:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:fo="http://www.w3.org/1999/xsl/format" > <xsl:output method="text" omit-xml-declaration="yes" indent="no"/> <xsl:template match="/">owner,address,citystate,yearbuilt <xsl:for-each select="//results> <xsl:value-of select="concat(ownername,',',houseaddress,',',houseaddress,',',yearbuilt,'
')"/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
that gave me:
owner,address,citystate,yearbuilt name1,house1,house1,year1 name2,house2,house2,year2
is there trick this? can't attribute names changed originator i'm stuck them. thank in advance.
use:
houseaddress[2]
to value of second occurrence of houseaddress
element.
note assuming xslt 1.0 here.
Comments
Post a Comment