2

这是一个示例 XML 文件:

<PubmedArticleSet>
<PubmedArticle>
 <MedlineCitation Owner="NLM" Status="MEDLINE">
    <PMID Version="1">23458631</PMID>
    <DateCreated>
        <Year>2013</Year>
        <Month>04</Month>
        <Day>08</Day>
    </DateCreated>
    <MeshHeadingList>
        <MeshHeading>
            <DescriptorName MajorTopicYN="N">Animals</DescriptorName>
        </MeshHeading>
        <MeshHeading>
            <DescriptorName MajorTopicYN="N">Calcium</DescriptorName>
            <QualifierName MajorTopicYN="Y">metabolism</QualifierName>
        </MeshHeading>
        <MeshHeading>
            <DescriptorName MajorTopicYN="N">Calcium Chloride</DescriptorName>
            <QualifierName MajorTopicYN="N">administration &amp; dosage</QualifierName>
        </MeshHeading>
     </MeshHeadingList>
 </MedlineCitation>
</PubmedArticle>
<PubmedArticle>
 <MedlineCitation Status="Publisher" Owner="NLM">
    <PMID Version="1">23458629</PMID>
    <DateCreated>
        <Year>2013</Year>
        <Month>3</Month>
        <Day>20</Day>
    </DateCreated>
    <MeshHeadingList>
        <MeshHeading>
            <DescriptorName MajorTopicYN="N">Adolescent</DescriptorName>
        </MeshHeading>
        <MeshHeading>
            <DescriptorName MajorTopicYN="N">Adult</DescriptorName>
        </MeshHeading>
        <MeshHeading>
            <DescriptorName MajorTopicYN="N">Anthropometry</DescriptorName>
        </MeshHeading>
     </MeshHeadingList>
 </MedlineCitation>
</PubmedArticle>
</PubmedArticleSet>

我想使用 XSL 解析 XMl 文件并提取每篇文章的 PMID、DateCreated、所有 DescriptorName 和 MajorTopicYN。然后,将结果保存在 csv 文件中,如下所示:

ArticleID|CreatedDate|MeSH|IsMajor
23458631|20130408|Animals|N
23458631|20130408|Calcium|N
23458631|20130408|Calcium Chloride|N
23458629|20130320|Adolescent|N
23458629|20130320|Adult|N
23458629|20130320|Anthropometry|N

谢谢。

4

1 回答 1

2

这是你想要的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:variable name="newline">
        <xsl:text>&#10;</xsl:text>
    </xsl:variable>

    <xsl:variable name="carriagereturn">
        <xsl:text>&#13;</xsl:text>
    </xsl:variable>

    <xsl:template match="@*|node()">
        <xsl:apply-templates select="@*|node()" />
    </xsl:template>

    <xsl:template match="/">
        <xsl:text>ArticleID|CreatedDate|MeSH|IsMajor</xsl:text>
        <xsl:value-of select="$carriagereturn" />

        <xsl:apply-templates select="@*|node()" />
    </xsl:template>

    <xsl:template match="DescriptorName">
        <xsl:value-of select="ancestor::MedlineCitation/PMID" />
        <xsl:text>|</xsl:text>

        <xsl:value-of select="ancestor::MedlineCitation/DateCreated/Year" />
        <xsl:value-of select="ancestor::MedlineCitation/DateCreated/Month" />
        <xsl:value-of select="ancestor::MedlineCitation/DateCreated/Day" />
        <xsl:text>|</xsl:text>

        <xsl:value-of select="." />
        <xsl:text>|</xsl:text>

        <xsl:value-of select="@MajorTopicYN" />

        <xsl:value-of select="$carriagereturn" />
    </xsl:template>
</xsl:stylesheet>

祝你好运!

于 2013-10-29T15:54:40.177 回答