2017-03-17 84 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。这是我必须要做的第一件事。 –