最好使用XSLT如何提取XML文档的文本内容。
对于这样的片段
<record>
<tag1>textual content</tag1>
<tag2>textual content</tag2>
<tag2>textual content</tag2>
</record>
理想的结果是:
文字内容文字内容文字内容
内容可处理以进行进一步操作(例如文本挖掘)的最佳输出格式(表格,CSV等)是什么?
谢谢
更新
为了扩展这个问题,如何分别提取每个记录的内容。例如,对于以下XML:
<Records>
<record id="1">
<tag1>textual co</tag1>
<tag2>textual con</tag2>
<tag2>textual cont</tag2>
</record>
<record id="2">
<tag1>some text</tag1>
<tag2>some tex</tag2>
<tag2>some te</tag2>
</record>
</Records>
所需的结果应为:
(textual co, textual con, textual cont) , (some text, some tex, some te)
或采用更好的格式以进行进一步的处理操作。
您可以使用以下XSLT:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="//text()"/>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">, </xsl:if>
</xsl:template>
</xsl:transform>
对于问题中的更新,可以使用以下XSLT:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">(<xsl:apply-templates select=".//text()"/>)<xsl:if test="position() != last()">, </xsl:if>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">, </xsl:if>
</xsl:template>
</xsl:transform>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句