我有一个XML文件,格式如下:
<row>
<field name="id">18</field>
<field name="lat">40.7560540000</field>
<field name="lng">-73.9869510000</field>
<field name="body">Students arriving from Haiti say they’re having a hard time getting placed in city schools.</field>
</row>
我希望将这些多个标记组合在一起并获取此表单的XML(将多个标记组合到单个标记的属性中):
<row id="18" lat="40.7560540000" lng="-73.9869510000" body="Students arriving from Haiti say they’re having a hard time getting placed in city schools.">
</row>
有可能这样做吗?如果有,有人可以建议我如何进行这样的转变吗?提前谢谢。
答案 0 :(得分:0)
是的,这样的改变是可能的。一种简单的方法(取决于你如何看待它)是对它应用XML转换:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<!-- the document element gets copied -->
<xsl:template match="/*">
<xsl:copy>
<!-- transform any child <row> elements -->
<xsl:apply-templates select="row" />
</xsl:copy>
</xsl:template>
<!-- <row> elements get copied, too -->
<xsl:template match="row">
<xsl:copy>
<!-- transform any child <field> elements -->
<xsl:apply-templates select="field" />
</xsl:copy>
</xsl:template>
<!-- <field> elements become attributes -->
<xsl:template match="field">
<xsl:attribute name="{@name}">
<!-- the value of the <field> becomes the attribute value -->
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
适用于:
<rows>
<row>
<field name="id">18</field>
<field name="lat">40.7560540000</field>
<field name="lng">-73.9869510000</field>
<field name="body">Students arriving from Haiti say they’re having a hard time getting placed in city schools.</field>
</row>
</rows>
你得到:
<rows>
<row id="18" lat="40.7560540000" lng="-73.9869510000" body="Students arriving from Haiti say they’re having a hard time getting placed in city schools."></row>
</rows>