1

我正在尝试对以下 xml 的每两个 tei:div 元素进行分组(这是包含我的简历的较大文件的一部分),并将它们包装在一个<div type="reference_block">

     <div type="category">
        <head n="references">References</head>
        <div type="reference" xml:id="1">
              <persName>
                 <forename type="first">firstname1</forename>
                 <surname>surname1</surname>
              </persName> 
           <address>
                 <name type="building">building1</name>
                 <postBox>postbox1</postBox>
                 <placeName>
                    <settlement>settlement1</settlement>
                    <region>region1</region>
                    <country>country1</country>
                 </placeName>
                 <postCode>postcode1</postCode>
           </address>
           <num n="telephone">telephone1</num>
           <email>email1</email>
        </div>
        <div type="reference" xml:id="2">
              <persName>
                 <forename type="first">firstname2</forename>
                 <surname>surname2</surname>
              </persName> 
           <address>
                 <name type="building">building2</name>
                 <postBox>postbox2</postBox>
                 <placeName>
                    <settlement>settlement2</settlement>
                    <region>region2</region>
                    <country>country2</country>
                 </placeName>
                 <postCode>postcode2</postCode>
           </address>
           <num n="telephone">telephone2</num>
           <email>email2</email>
        </div>
        <div type="reference" xml:id="3">
              <persName>
                 <forename type="first">firstname3</forename>
                 <surname>surname3</surname>
              </persName> 
           <address>
                 <name type="building">building3</name>
                 <postBox>postbox3</postBox>
                 <placeName>
                    <settlement>settlement3</settlement>
                    <region>region3</region>
                    <country>country3</country>
                 </placeName>
                 <postCode>postcode3</postCode>
           </address>
           <num n="telephone">telephone3</num>
           <email>email3</email>
        </div>
        <div type="reference" xml:id="4">
              <persName>
                 <forename type="first">firstname4</forename>
                 <surname>surname4</surname>
              </persName> 
           <address>
                 <name type="building">building4</name>
                 <postBox>postbox4</postBox>
                 <placeName>
                    <settlement>settlement4</settlement>
                    <region>region4</region>
                    <country>country4</country>
                 </placeName>
                 <postCode>postcode4</postCode>
           </address>
           <num n="telephone">telephone4</num>
           <email>email4</email>
        </div>
        <div type="reference" xml:id="5">
              <persName>
                 <forename type="first">firstname5</forename>
                 <surname>surname5</surname>
              </persName> 
           <address>
                 <name type="building">building5</name>
                 <postBox>postbox5</postBox>
                 <placeName>
                    <settlement>settlement5</settlement>
                    <region>region5</region>
                    <country>country5</country>
                 </placeName>
                 <postCode>postcode5</postCode>
           </address>
           <num n="telephone">telephone5</num>
           <email>email5</email>
        </div>
        <div type="reference" xml:id="6">
              <persName>
                 <forename type="first">firstname6</forename>
                 <surname>surname6</surname>
              </persName>
           <address>
                 <name type="building">building6</name>
                 <postBox>postbox6</postBox>
                 <placeName>
                    <settlement>settlement6</settlement>
                    <region>region6</region>
                    <country>country6</country>
                 </placeName>
                 <postCode>postcode6</postCode>
           </address>
           <num n="telephone">telephone6</num>
           <email>email6</email>
        </div>
        <div type="reference" xml:id="7">
              <persName>
                 <forename type="first">firstname7</forename>
                 <surname>surname7</surname>
              </persName>
           <address>
                 <name type="building">building7</name>
                 <postBox>postbox7</postBox>
                 <placeName>
                    <settlement>settlement7</settlement>
                    <region>region7</region>
                    <country>country7</country>
                 </placeName>
                 <postCode>postcode7</postCode>
           </address>
           <num n="telephone">telephone7</num>
           <email>email7</email>
        </div>
     </div>

我正在使用此模板尝试包装项目:

 <xsl:template match="tei:div[@type='reference']">
    <xsl:if test="@xml:id mod 2 = 1">
        <div class="reference_block">
            <xsl:apply-templates select=".|following-sibling::tei:div[@type='reference'][@xml:id &lt; 2]"/>
        </div>
       </xsl:if>
    </xsl:template>

但是,当我这样做时,我收到一条错误消息:“嵌套的应用模板调用过多。样式表可能正在循环”

问题似乎是“。” 在我的选择中,但我不确定为什么会收到错误消息。也许是因为它在呼唤自己而我没有意识到?有人能给我一些关于为什么会发生这种情况的见解,或者另一种方法来对不需要“。”的项目进行分组。选择?谢谢!

4

1 回答 1

1

"."确实是导致问题的原因,因为它代表当前的上下文节点(即您当前匹配的div节点),因此选择它只会导致匹配相同的模板,并且在模板中test="@xml:id mod 2 = 1"仍然是真的,领先到无限递归。

如果您只想复制当前div和以下div,只需将xsl:apply-templates替换为xsl:copy-of即可。请注意,您获取以下兄弟的表达方式也不完全正确。您想将下一个元素的id与当前元素的id进行比较

尝试这个...

 <xsl:copy-of select=".|following-sibling::tei:div[@type='reference'][@xml:id &lt; current()/@xml:id + 2]"/>

或者,如果 id 总是有序的,这也可以

<xsl:copy-of select=".|following-sibling::tei:div[@type='reference'][1]"/>

另一方面,如果你想对你选择的一对div元素做一些进一步的转换,你可以使用mode属性,这样可以匹配一个不同的模板

<xsl:template match="tei:div[@type='reference']">
   <xsl:if test="@xml:id mod 2 = 1">
      <div class="reference_block">
        <xsl:apply-templates select=".|following-sibling::tei:div[@type='reference'][1]" mode="group"/>
      </div>
   </xsl:if>
</xsl:template>

<xsl:template match="tei:div" mode="group">
    <!-- Other code here -->
</xsl:template>
于 2014-07-17T17:06:21.273 回答