提取XML文档的文本内容 [英] Extracting textual content of XML documents

查看:96
本文介绍了提取XML文档的文本内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于所需算法的特定要求,我应该以类似表的格式准备输入,在该格式中不可能有具有多个数据项的单元格。简单来说,对于doc如下(这是完整文档的片段):



  <  文章 >  
< 文章 >
< 作者 > Frank Manola < / author >
< title > title1 < / title >
< 期刊 > GTE < / journal >
< > December < / month >
< > 1991 < / year >
< / article >

< article >
< 作者 > Frank Manola < / author >
< 作者 < span class =code-keyword>> Sandra Heiler < / author < span class =code-keyword>>
< title > title2 < / title < span class =code-keyword>>
< journal > ASF < / journal < span class =code-keyword>>
< month > 八月< / month >
< year > 1993 < / year >
< / article >
< / Articles >





所需的输出如下:

Frank Manola,title1,GTE,1991年12月

Frank Manola,title2,ASF ,1993年8月

Sandra Heiler,标题2,ASF,1993年8月





事实上,对于thos具有多个作者(作者标签)的e记录(有4个或更多个实例),每个应该在单独的行中检索。



如何使用XSLT做到这一点?



更新:这是一个接近我想要的解决方案:



 <   xsl:transform     version   =  1.0    xmlns:xsl   =  http://www.w3.org/1999/XSL/Transform >  
< xsl:output 方法 = text 缩进 = / >
< xsl:strip-space elements = * / >
< xsl:template 匹配 = / * >
< xsl:apply-templates / >
< / xsl:template >
< xsl:template 匹配 = < span class =code-keyword> * > < xsl:apply-templates select = .// text() / > < xsl:if test = position()!= last() > < / xsl:if >
< / xsl:template >
< ; xsl:template match = text() >
< xsl:value-of 选择 = / >
< xsl:if test = position()!= last() > < / xsl:if >
< / xsl:template >
< / xsl:transform >

解决方案

这样的事情应该有效:

 <   xsl:transform    < span class =code-attribute> version   =  1.0    xmlns:xsl   =  http://www.w3.org/1999/XSL/Transform >  
< xsl:output 方法 = text 缩进 = / >
< xsl:strip-space elements = * / >

< xsl :template 匹配 = / >
< xsl:apply-templates / >
< / xsl:template >

< xsl:template 匹配 = 文章 >
< xsl:apply-templates 选择 = 作者 / >
< xsl:if test = position()!= last() > < / xsl:if >
< / xsl:template >

< xsl:template mat ch = 作者 > < xsl:apply-templates 选择 = ./ text() / > < xsl:apply-templates 选择 = ../ title / text() / > < xsl:apply -templates 选择 = ../ journal / text() / > < xsl:apply-templates 选择 = ../ month / text() / > < xsl:apply-templates 选择 = ../ year / text() / > < xsl:if test = position()!= last() > < / xsl:if > < / xsl:template >
< / xsl:transform >


For specific requirements of the desired algorithm, I should prepare the input in a table-like format in which it is not possible to have cells with more than one data item. Simply, for doc as follows (this is a fragment of complete 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>



the desired output is such as follows:

Frank Manola, title1, GTE, December, 1991

Frank Manola, title2, ASF, August, 1993

Sandra Heiler, title2, ASF, August, 1993



In fact, for those records that have more than one authors (author tag) (there are instances with 4 or more ones), each one should be retrieved in a separate line.

How to do that using XSLT?

Update: This is a solution that is close to what I want:

<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>

解决方案

Something like this should work:

<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="article">
        <xsl:apply-templates select="author"/>
        <xsl:if test="position() != last()">, </xsl:if>
    </xsl:template>

    <xsl:template match="author">(<xsl:apply-templates select="./text()"/>, <xsl:apply-templates select="../title/text()"/>, <xsl:apply-templates select="../journal/text()"/>, <xsl:apply-templates select="../month/text()"/>, <xsl:apply-templates select="../year/text()"/>)<xsl:if test="position() != last()">, </xsl:if></xsl:template>
</xsl:transform>


这篇关于提取XML文档的文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆