检索关于特定标签的 XML 个文档的内容
Retrieving content of XML documents regarding a specific tag
首先抱歉标题可能不准确!
检索或提取 XML 文档的内容是一个常见问题,已在 SO 中的其他帖子中得到解答。
对于所需算法的特定要求,我应该以 table-like 格式准备输入,在这种格式中,不可能有包含多个数据项的单元格。简单来说,对于doc如下(这是完整doc的一个片段):
<Articles>
<article>
<author>Frank Manola</author>
<title>title1</title>
<journal>GTE</journal>
<month>December</month>
<year>1991</year>
</article>
<article>
<author>Frank Manola</author>
<author>Sandra Heiler</author>
<title>title2</title>
<journal>ASF</journal>
<month>August</month>
<year>1993</year>
</article>
</Articles>
期望的输出如下:
Frank Manola, title1, GTE, December, 1991
Frank Manola, title2, ASF, August, 1993
Sandra Heiler, title2, ASF, August, 1993
事实上,对于那些有多个作者(作者标签)的记录(有 4 个或更多作者的实例),每个作者都应该在单独的行中检索。
如何使用 XSLT 做到这一点?
谢谢,
下面的 XSLT 应该可以满足您的要求:
<xsl:output method="text" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Articles">
<xsl:for-each select="article/author">
<xsl:value-of select="."/>, <xsl:value-of select="../title" />, <xsl:value-of select="../journal" />, <xsl:value-of select="../year" /><xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
首先抱歉标题可能不准确!
检索或提取 XML 文档的内容是一个常见问题,已在 SO 中的其他帖子中得到解答。
对于所需算法的特定要求,我应该以 table-like 格式准备输入,在这种格式中,不可能有包含多个数据项的单元格。简单来说,对于doc如下(这是完整doc的一个片段):
<Articles>
<article>
<author>Frank Manola</author>
<title>title1</title>
<journal>GTE</journal>
<month>December</month>
<year>1991</year>
</article>
<article>
<author>Frank Manola</author>
<author>Sandra Heiler</author>
<title>title2</title>
<journal>ASF</journal>
<month>August</month>
<year>1993</year>
</article>
</Articles>
期望的输出如下:
Frank Manola, title1, GTE, December, 1991
Frank Manola, title2, ASF, August, 1993
Sandra Heiler, title2, ASF, August, 1993
事实上,对于那些有多个作者(作者标签)的记录(有 4 个或更多作者的实例),每个作者都应该在单独的行中检索。
如何使用 XSLT 做到这一点?
谢谢,
下面的 XSLT 应该可以满足您的要求:
<xsl:output method="text" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Articles">
<xsl:for-each select="article/author">
<xsl:value-of select="."/>, <xsl:value-of select="../title" />, <xsl:value-of select="../journal" />, <xsl:value-of select="../year" /><xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>