我正在使用XSLT將XML文檔從一種文檔類型轉換爲另一種文檔類型。在簡化形式,我有一些看起來像這樣:在XSLT中放置一組元素的包裝
<section>
<title>Title</title>
<a>content1</a>
<a>content2</a>
<a>content3</a>
<a>content4</a>
<b>other content</b>
<a>content5</a>
<a>content6</a>
<a>content7</a>
<c>other content</c>
<a>content8</a>
<a>content9</a>
<a>content10</a>
</section>
我想是把周圍<a>
單元的組的包裝,所以翻譯的結果是這樣的:
<section>
<title>Title</title>
<a-wrapper>
<a>content1</a>
<a>content2</a>
<a>content3</a>
<a>content4</a>
</a-wrapper>
<b>other content</b>
<a-wrapper>
<a>content5</a>
<a>content6</a>
<a>content7</a>
</a-wrapper>
<c>other content</c>
<a-wrapper>
<a>content8</a>
<a>content9</a>
<a>content10</a>
</a-wrapper>
</section>
我目前的模板看起來像這樣:
<xsl:template match="a">
<xsl:choose>
<xsl:when test="not(preceding-sibling::a)">
<xsl:element name="a-wrapper">
<xsl:element name="a">
<xsl:apply-templates/>
</xsl:element>
<xsl:apply-templates select="following-sibling::a[generate-id(preceding-sibling::a[last()]) = generate-id(current())]"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="a">
<xsl:apply-templates/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
這讓我在那裏。問題是,apply-templates標籤中的select子句獲取所有的<a>
標籤,當我希望它在第一個非<a>
標籤處停止時,即第一個標籤的處理將所有10個<a>
標籤在第一個<a-wrapper>
內,當我只想要前四個時。這也有插入嵌套的<a-wrapper>
標籤的效果,如第五個標籤生成的標籤,這是DTD所不允許的。
所以問題是,如何選擇以第一個非<a>
標籤結尾的以下兄弟的子集?
我其實*不想*包裹除以外的任何東西。但我應該能夠取消通用,只適用於標籤。我會做一些測試並確認。 – 2013-05-06 13:52:45
對不起,我的錯誤是沒有正確閱讀這個問題。我已經修改了我的答案,以說明如何做到這一點。 – 2013-05-07 07:14:47