2017-02-09 61 views
0

如何将'affiliation'元素移动到XSLT中'for-each'上下文中的'contrib'元素中?如何将元素移动到XSLT中的另一个变换元素中?

输入XML:

<article> 
    <authors>Amrendra, Kumar; Mohan, Kumar</authors> 
    <affiliation id="Amrendra, Kumar">Amrendra, Kumar</affiliation> 
    <affiliation id="Mohan, Kumar">Mohan, Kumar</affiliation> 
</article> 

电流输出:

<contrib-group> 
    <contrib> 
    <string-name>Amrendra, Kumar</string-name> 
    </contrib> 
    <contrib> 
    <string-name>Mohan, Kumar</string-name> 
    </contrib> 
    <aff id="Amrendra, Kumar">Amrendra, Kumar</aff> 
    <aff id="Mohan, Kumar">Mohan, Kumar</aff> 
</contrib-group> 

XSLT代码:

<xsl:template match="authors"> 
    <xsl:choose> 
     <xsl:when test="not(node())"/> 
     <xsl:otherwise> 
      <contrib-group> 
       <xsl:for-each select="tokenize(., ';')"> 
       <contrib> 
        <string-name> 
         <xsl:value-of select="normalize-space(.)"/> 
        </string-name> 
       </contrib> 
       </xsl:for-each> 
       <xsl:apply-templates select="../affiliation"/> 
      </contrib-group> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

所需的输出:(带AFF/@ ID和contrib请匹配/#PCDATA )

<contrib-group> 
    <contrib> 
    <string-name>Amrendra, Kumar</string-name> 
    <aff id="Amrendra, Kumar">Amrendra, Kumar</aff> 
    </contrib> 
    <contrib> 
    <string-name>Mohan, Kumar</string-name> 
    <aff id="Mohan, Kumar">Mohan, Kumar</aff> 
    </contrib> 
</contrib-group> 

当我尝试应用里面的contrib“归属”元素,则其显示如下错误:

[Saxon-PE 9.5.1.7] XPTY0020: Cannot select the parent of the context node: the context item is not a node 
+0

你能证明你的演示问题,请完整的XSLT?我不认为所显示的模板会导致错误,除非您的实际XSLT在'xsl:for-each'结构中具有'',不是在它之后。谢谢。 –

+0

作者和相应的“隶属关系”之间有什么联系?你在3个地方有相同的值,很难说出哪一对要用。 –

回答

0

假设authors字符串包含令牌相同的affiliation/@id值,你可以这样做:

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="affiliation" use="@id" /> 

<xsl:template match="article"> 
    <xsl:variable name="current-article" select="." /> 
    <contrib-group> 
     <xsl:for-each select="tokenize(authors, '; ')"> 
      <contrib> 
       <string-name> 
        <xsl:value-of select="."/> 
       </string-name> 
       <xsl:apply-templates select="key('aff', ., $current-article)[node()]"/> 
      </contrib> 
     </xsl:for-each> 
    </contrib-group> 
</xsl:template> 

<xsl:template match="affiliation"> 
    <aff> 
     <xsl:copy-of select="@* | node()" /> 
    </aff> 
</xsl:template> 

</xsl:stylesheet> 

获得日e需要的输出。

+0

谢谢。现在我得到了结果,但总是与@id空的aff来。我在这里有两个更多的关注: 1)如果我们有一些数据在“隶属关系”内。当前逻辑使空。 2)如果'归属'空不需要产生'aff'? –

+0

@AmrendraKumar我做了一些改变,看看是否满足你的顾虑。 –

0
<aff id="{.}"> 
    <xsl:value-of select="key('aff', ., $current-article)" /> 
</aff> 

应与变化,以避免空间和遗漏值

<aff id="{normalize-space(.)}"> 
    <xsl:value-of select="key('aff', normalize-space(.), $current-article)" /> 
</aff> 
+0

谢谢!我结合了你的评论,然后我知道了。但是如果'从属关系'为空(没有PCDATA),那么我需要再次进行更改,然后放弃'关联'。再次感谢 –

相关问题