我有以下XML。如何通過XSLT移動元素
<body>
<p type="Heading 1">My Heading</p>
<p>This is paragraph Text... This is paragraph text... <p type="Key Words">This is a keyword A</p></p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text... <p type="Key Words">This is a keyword B</p></p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p type="Heading 1">My Next Heading</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... <p type="Key Words">This is a keyword C</p>This is paragraph text...</p>
<p type="Heading 2">My Next Heading</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... <p type="Key Words">This is a keyword D</p> This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
</body>
只是下一標題之前,如下圖所示
我想移動所有的「關鍵詞」的:
<body>
<p type="Heading 1">My Heading</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p type="Key Words">This is a keyword A</p>
<p type="Key Words">This is a keyword B</p>
<p type="Heading 1">My Next Heading</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p type="Key Words">This is a keyword C</p>
<p type="Heading 2">My Next Heading</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p>This is paragraph Text... This is paragraph text...</p>
<p type="Key Words">This is a keyword D</p>
</body>
我有工作的代碼,但它有嚴重的性能問題,因爲我我正在對數十萬字的文檔進行這種轉換。以下是我目前的代碼。
<!-- Place all keywords in section right before the next heading title. -->
<xsl:template match="p[contains(@type,'Heading')]">
<xsl:variable name="headingCount" >
<xsl:value-of select="count(preceding::p[contains(@type,'Heading')])"/>
</xsl:variable>
<xsl:variable name="precedingKeyWordCount">
<xsl:value-of select="count(preceding::p[contains(@type,'Key Words') and count(preceding::p[contains(@type,'Heading')]) = $headingCount])"/>
</xsl:variable>
<xsl:if test="$precedingKeyWordCount > 0" >
<p type="Key Words">
<xsl:apply-templates select="preceding::p[contains(@type,'Key Words') and count(preceding::p[contains(@type,'Heading')]) = $headingCount]" />
</p>
</xsl:if>
<!-- place original heading -->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
有沒有人知道更有效的方法來實現這一目標?
謝謝。
謝謝,但我需要一個不依賴於結構體/ p/p的解決方案。 – joe
好吧,我想我可以馴服它。謝謝您的幫助! – joe