XSLT:将id从一个文档分配给另一个文档,迭代多个相同的值

时间:2016-06-24 12:50:24

标签: xml xslt position xslt-2.0

任何人都可以帮我解决以下问题吗?我有两个XML文档。一个看起来像这样的TEI:

<?xml version="1.0" encoding="UTF-8"?>
<text>
    <body>
        <div>
            <head>IN ADVENTU DOMINI</head>
            <div type="time:1">
                <ab ana="#head">
                    <hi>aaaa</hi>
                </ab>
            </div>
        </div>
        <div>
            <head>FERIA II.</head>
            <div type="time:1">
                <ab ana="#head">
                    <hi>bbbb</hi>
                </ab>
            </div>
        </div>
        <div>
            <head>FERIA III.</head>
            <div type="time:1">
                <ab ana="#head">
                    <hi>cccc</hi>
                </ab>
            </div>
        </div>
        <div>
            <head>DOMINICA</head>
            <div type="time:1">
                <ab ana="#head">
                    <hi>dddd</hi>
                </ab>
            </div>
        </div>
        <div>
            <head>FERIA II.</head>
            <div type="time:1">
                <ab ana="#head">
                    <hi>eeee</hi>
                </ab>
            </div>
        </div>
        <div>
            <head>FERIA III.</head>
            <div type="time:1">
                <ab ana="#head">
                    <hi>ffff</hi>
                </ab>
            </div>
        </div>
    </body>
</text>

和一个看起来像这样的表:

<table>
        <row>
            <cell>IN ADVENTU DOMINI</cell>
            <cell>1234</cell>
        </row>
        <row>
            <cell>FERIA II.</cell>
            <cell>1200</cell>
        </row>
        <row>
            <cell>FERIA III.</cell>
            <cell>1211</cell>
        </row>
        <row>
            <cell>DOMINICA</cell>
            <cell>1299</cell>
        </row>
        <row>
            <cell>FERIA II.</cell>
            <cell>9999</cell>
        </row>
        <row>
            <cell>FERIA III.</cell>
            <cell>8888</cell>
        </row>
</table>

现在我想将id(在表的第二个<cell>元素中找到)添加到TEI中与<head>中具有相同值的那些元素中,与第一个<cell>中的值相同表的元素。我写了以下XSLT:

<xsl:template match="t:head">
        <xsl:variable name="pos" select="count(current())"/>
        <xsl:variable name="ids" select="document('feasts.xml')"/>
        <xsl:variable name="row" select="$ids//row[cell[1]=current()][position()=$pos]"/>

            <xsl:copy>
                <xsl:if test="$row!=''">
                    <xsl:attribute name="xml:id">
                        <xsl:text>CDB.</xsl:text><xsl:value-of select="$row/cell[2]"/>
                    </xsl:attribute>
                </xsl:if>
                <xsl:apply-templates select="*|@*|text()"/>
            </xsl:copy>

 </xsl:template>

我知道我必须对$ pos变量做一些事情,但我不知道如何从表中获取正确的ID,而不仅仅是第一个。 <head>元素的所需输出为:

        <head xml:id="CDB.1234">IN ADVENTU DOMINI</head>

        <head xml:id="CDB.1200">FERIA II.</head>

        <head xml:id="CDB.1211">FERIA III.</head>

        <head xml:id="CDB.1299">DOMINICA</head>

        <head xml:id="CDB.9999">FERIA II.</head>

        <head xml:id="CDB.8888">FERIA III.</head>

1 个答案:

答案 0 :(得分:2)

最好使用来解决交叉引用:

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:param name="ids" select="document('feasts.xml')"/>

<xsl:key name="grp" match="head" use="." />
<xsl:key name="id" match="row" use="cell[1]" />

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

<xsl:template match="head">
    <xsl:variable name="i" select="index-of(key('grp', .)/generate-id(), generate-id())"/>
    <head xml:id="CDB.{key('id', ., $ids)[$i]/cell[2]}">
        <xsl:value-of select="."/>
    </head>
 </xsl:template>

</xsl:stylesheet>

应用于您的输入示例,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<text>
   <body>
      <div>
         <head xml:id="CDB.1234">IN ADVENTU DOMINI</head>
         <div type="time:1">
            <ab ana="#head">
               <hi>aaaa</hi>
            </ab>
         </div>
      </div>
      <div>
         <head xml:id="CDB.1200">FERIA II.</head>
         <div type="time:1">
            <ab ana="#head">
               <hi>bbbb</hi>
            </ab>
         </div>
      </div>
      <div>
         <head xml:id="CDB.1211">FERIA III.</head>
         <div type="time:1">
            <ab ana="#head">
               <hi>cccc</hi>
            </ab>
         </div>
      </div>
      <div>
         <head xml:id="CDB.1299">DOMINICA</head>
         <div type="time:1">
            <ab ana="#head">
               <hi>dddd</hi>
            </ab>
         </div>
      </div>
      <div>
         <head xml:id="CDB.9999">FERIA II.</head>
         <div type="time:1">
            <ab ana="#head">
               <hi>eeee</hi>
            </ab>
         </div>
      </div>
      <div>
         <head xml:id="CDB.8888">FERIA III.</head>
         <div type="time:1">
            <ab ana="#head">
               <hi>ffff</hi>
            </ab>
         </div>
      </div>
   </body>
</text>