我试图对以下xml的每两个tei:div元素进行分组(这是包含我的CV的较大文件的一部分),并将它们包装在<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 < 2]"/>
</div>
</xsl:if>
</xsl:template>
然而,当我这样做时,我收到一条错误消息:&#34;太多嵌套的应用模板调用。样式表可能正在循环播放&#34;
问题似乎是&#34;。&#34;在我的选择中,但我不确定为什么我收到错误消息。也许是因为它自称并且我没有意识到这一点?有人可以让我深入了解为什么会发生这种情况,或者另外一种方法可以对那些不需要&#34;。&#34;的项目进行分组。选择?谢谢!
答案 0 :(得分: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 < current()/@xml:id + 2]"/>
或者,如果id总是按顺序排列,那么这也会起作用
<xsl:copy-of select=".|following-sibling::tei:div[@type='reference'][1]"/>
另一方面,如果你想对你选择的 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'][1]" mode="group"/>
</div>
</xsl:if>
</xsl:template>
<xsl:template match="tei:div" mode="group">
<!-- Other code here -->
</xsl:template>