2017-03-17 99 views
0

我有一個包含轉化TEI峯值到LG

<div> 
    <p> 
    some text, and maybe nodes <note>A note</note><lb /> 
    and some more text<lb /> 
    final line without lb 
    </p> 
</div> 

TEI(文本編碼倡議)文檔,我想將它轉化到:通過使用

<div> 
    <lg> 
    <l>some text, and maybe nodes <note>A note</note></l> 
    <l>and some more text</l> 
    <l>final line without lb</l> 
    </lg> 
</div> 

轉化所述p到LG是微不足道

<xsl:template match="tei:div/tei:p"> 
    <lg> 
    <xsl:apply-templates/> 
    </lg> 
</xsl:template> 

但其餘的我不知道該怎麼做。將一系列節點轉換爲新父項的子項。

如果有xslt 1.0的解決方案,它會很棒。

回答

0

您可以在這裏使用一種叫做Muenchian grouping的技術。在這種情況下,你可以按下面這些lb元素的數量p元素的子節點

<xsl:key name="p-nodes" match="tei:p/node()" use="concat(generate-id(..), '|', count(following-sibling::tei:lb))" /> 

要獲得各組的第一個節點,這將表示要輸出的,你會每l選擇它們是這樣的...

<xsl:for-each 
    select="node()[generate-id() = generate-id(key('p-nodes', concat($parentId, '|', count(following-sibling::tei:lb)))[1])]"> 

,並輸出<l>標籤本身和組的內容,再次用鑰匙...

<l><xsl:apply-templates select="key('p-nodes', concat($parentId, '|', count(following-sibling::tei:lb)))[not(self::tei:lb)]" /></l> 

試試這個XSLT(顯然改變了命名空間爲tei前綴匹配你的XML真鈔)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:tei="tei"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:key name="p-nodes" match="tei:p/node()" use="concat(generate-id(..), '|', count(following-sibling::tei:lb))" /> 

    <xsl:template match="tei:div/tei:p"> 
     <lg> 
      <xsl:variable name="parentId" select="generate-id()" /> 
      <xsl:for-each select="node()[generate-id() = generate-id(key('p-nodes', concat($parentId, '|', count(following-sibling::tei:lb)))[1])]"> 
       <l><xsl:apply-templates select="key('p-nodes', concat($parentId, '|', count(following-sibling::tei:lb)))[not(self::tei:lb)]" /></l> 
      </xsl:for-each> 
     </lg> 
    </xsl:template> 

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

http://xsltransform.net/gWEamMf

+0

嗨。非常感謝@ tim-c :)我想我還有很多要學習xslt。 –

+0

你可能也想看看michael.hor257K的回答。它更整潔,更高效。 –

1

看到它在行動這裏的另一種方式,你可以看看它。它使用將每個節點鏈接到其最近的前一個lb分隔符。這使您可以通過領先的分離器的唯一ID來獲取各組(除最前面的一個):

XSLT 1.0

<xsl:stylesheet version="1.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="*"/> 

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

<xsl:key name="following-nodes" match="node()[not(self::lb)]" use="generate-id(preceding-sibling::lb[1])" /> 

<xsl:template match="p[lb]"> 
    <lg> 
     <l> 
      <xsl:apply-templates select="lb[1]/preceding-sibling::node()"/> 
     </l> 
     <xsl:for-each select="lb"> 
      <l> 
       <xsl:apply-templates select="key('following-nodes', generate-id())"/> 
      </l> 
     </xsl:for-each> 
    </lg> 
</xsl:template> 

</xsl:stylesheet> 

此示例使用沒有命名空間,因爲你的問題沒有定義他們。

+0

非常感謝@ michael.hor257k :)我真的很喜歡xslt上的newbee。這是我必須要做的第一件事。 –