XSLT:从交叉引用中提取值

问题描述 投票:0回答:1

我正在尝试从与交叉引用 id 匹配的 aff 元素中提取值,但不确定使用什么 XPath 来获取特定的匹配值。

XML:

<article>    
<contrib-group>
    <contrib contrib-type="author">
    <name><surname>Doe</surname><given-names>Jane</given-names></name><email>email here</email><xref ref-type="aff" rid="affa">a</xref><xref ref-type="corresp" rid="cor2">*</xref>
    </contrib>
    <contrib contrib-type="author">
    <name><surname>Done</surname><given-names>John</given-names></name><email>email here</email><xref ref-type="aff" rid="affb">b</xref>
    </contrib>
    <aff id="affa"><label>a</label>Department of Philosophy, <institution>University of XXX</institution>, <country>Germany</country></aff>
    <aff id="affb"><label>b</label>Institute of Logic, <institution>Univeristy of YYY</institution>, Virginia, <country>United States</country></aff>
    </contrib-group>
</article>

XSLT:

<xsl:template match="*">
        <article>
        <xsl:apply-templates select="//contrib-group/contrib"/>
        </article>
    </xsl:template>
    
    <xsl:template match="article/contrib-group/contrib">
        <contrib>
        <surname><xsl:value-of select="name/surname"/></surname>
        <given-names><xsl:value-of select="name/given-names"/></given-names>
        <email><xsl:value-of select="./email"/></email>
        <affiliation>
            <xsl:if test="xref/@rid = following-sibling::aff/@id">
                <xsl:value-of select="following-sibling::aff"/>
            </xsl:if>
        </affiliation>
        </contrib>
    </xsl:template>

我知道这里的

<xsl:value-of>
不正确 - 如何选择与@rid匹配的相应链接@id?

输出为我提供了两个贡献的隶属关系:

<article>
    <contrib>
        <surname>Doe</surname>
        <given-names>Jane</given-names>
        <email>email here</email>
        <affiliation>aDepartment of Philosophy, University of XXX, Germany bInstitute of Logic, Ohio, United States</affiliation>
    </contrib>
    <contrib>
        <surname>Done</surname>
        <given-names>John</given-names>
        <email>Email here</email>
        <affiliation>aDepartment of Philosophy, University of XXX, Germany bInstitute of Logic, Ohio, United States</affiliation>
    </contrib>
</article>
xml xslt xslt-2.0 xpath-2.0
1个回答
0
投票

XSLT 有一个内置的 key 机制来解决交叉引用。

XSLT 2.0

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

<xsl:key name="aff" match="aff" use="@id" />

<xsl:template match="/article">
    <xsl:copy>
        <xsl:apply-templates select="contrib-group/contrib"/>
    </xsl:copy>
</xsl:template>
    
<xsl:template match="contrib">
    <xsl:copy>
        <xsl:copy-of select="name/* | email"/>
        <affiliation>
            <xsl:value-of select="key('aff', xref[@ref-type='aff']/@rid)/(node() except label)" separator=""/>
        </affiliation>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,这将返回:

结果

<?xml version="1.0" encoding="UTF-8"?>
<article>
   <contrib>
      <surname>Doe</surname>
      <given-names>Jane</given-names>
      <email>email here</email>
      <affiliation>Department of Philosophy, University of XXX, Germany</affiliation>
   </contrib>
   <contrib>
      <surname>Done</surname>
      <given-names>John</given-names>
      <email>email here</email>
      <affiliation>Institute of Logic, Univeristy of YYY, Virginia, United States</affiliation>
   </contrib>
</article>
© www.soinside.com 2019 - 2024. All rights reserved.