我有一个大型的xml文件(由Groundspeak.com生成),其中包含有关我所在地区位置的大量信息。
我希望能够将该数据的某些部分提取到新的csv文件,例如下面的例子。
<name>Isle of Man</name>
<desc>Geocache file generated by Groundspeak</desc>
<author>Groundspeak</author>
<bounds minlat="54.04725" minlon="-4.81115" maxlat="54.401" maxlon="-4.3105" />
<wpt lat="54.097833" lon="-4.6537">
<name>GC1E48M</name>
<desc>Hunter and Quarry by monstermunch, Traditional Cache (2/3)</desc>
<url>http://www.geocaching.com/seek/cache_details.aspx?guid=b357a623-25ed-43cc-aad5-9c0029231e0c</url>
<urlname>Hunter and Quarry</urlname>
<sym>Geocache</sym>
<type>Geocache|Traditional Cache</type>
<groundspeak:cache id="933587" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0/1">
<groundspeak:name>Hunter and Quarry</groundspeak:name>
<groundspeak:placed_by>monstermunch</groundspeak:placed_by>
<groundspeak:owner id="1283947">monstermunch</groundspeak:owner>
<groundspeak:type>Traditional Cache</groundspeak:type>
<groundspeak:container>Micro</groundspeak:container>
<groundspeak:attributes>
<groundspeak:attribute id="13" inc="0">Available at all times</groundspeak:attribute>
<groundspeak:attribute id="43" inc="1">Watch for livestock</groundspeak:attribute>
<groundspeak:attribute id="8" inc="1">Scenic view</groundspeak:attribute>
<groundspeak:attribute id="7" inc="1">Takes less than an hour</groundspeak:attribute>
<groundspeak:attribute id="39" inc="1">Thorns</groundspeak:attribute>
<groundspeak:attribute id="1" inc="1">Dogs</groundspeak:attribute>
</groundspeak:attributes>
<groundspeak:difficulty>2</groundspeak:difficulty>
<groundspeak:terrain>3</groundspeak:terrain>
<groundspeak:country>Isle of Man</groundspeak:country>
<groundspeak:state>
</groundspeak:state>
<groundspeak:short_description html="True">
</groundspeak:short_description>
<groundspeak:long_description html="True"><center><font size="+2" color="#006600" face=
"Arial"><strong>Hunter and Quarry</strong> is a traditional
cache which takes you on a stroll along a little used
footpath next to Billown Quarry. After walking through the..
我需要的只是<wpt lat="54.097833" lon="-4.6537">
和<desc>Hunter and Quarry by monstermunch, Traditional Cache (2/3)</desc>
采用以下格式:
-4.6537,54.097833,猎人和采石场
然后保存在csv文件中。
我在Mac上,有Sublime Text,不知道从哪里开始。
答案 0 :(得分:0)
使用此XSLT行获得所需的结果:
<xsl:value-of select="concat(wpt/@lon,',',wpt/@lat,',',wpt/urlname)" />
<强>结果:强>
-4.6537,54.097833,猎人和采石场
如果您还需要在输入中处理,
个字符,您可以使用以下命名模板CSVencode
(这比上面的简单解决方案稍微复杂一点):< / p>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<!-- Wrap Element -->
<xsl:template match="/root">
<xsl:call-template name="CSV_encode">
<xsl:with-param name="str" select="wpt/@lon" />
</xsl:call-template>
<xsl:text>,</xsl:text>
<xsl:call-template name="CSV_encode">
<xsl:with-param name="str" select="wpt/@lat" />
</xsl:call-template>
<xsl:text>,</xsl:text>
<xsl:call-template name="CSV_encode">
<xsl:with-param name="str" select="wpt/urlname" />
</xsl:call-template>
</xsl:template>
<xsl:template name="CSV_encode">
<xsl:param name="str" />
<xsl:variable name="str2">
<xsl:call-template name="Quot_encode">
<xsl:with-param name="str" select="$str" />
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($str2,',')">
<xsl:value-of select="concat('"',$str2,'"')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str2" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="Quot_encode">
<xsl:param name="str" />
<xsl:param name="outstr" />
<xsl:choose>
<xsl:when test="string-length($str) = 0">
<xsl:value-of select="$outstr" />
</xsl:when>
<xsl:when test="starts-with($str,'"')">
<xsl:call-template name="Quot_encode">
<xsl:with-param name="str" select="substring($str,2)" />
<xsl:with-param name="outstr" select="concat($outstr,'""')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="Quot_encode">
<xsl:with-param name="str" select="substring($str,2)" />
<xsl:with-param name="outstr" select="concat($outstr,substring($str,1,1))" />
</xsl:call-template> </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>