2017-02-09 79 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),那麼我需要再次進行更改,然後放棄'關聯'。再次感謝 –

相關問題