У меня есть следующий XML.
<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>
Iу меня есть код, который работает, но у него есть серьезные проблемы с производительностью, потому что я выполняю это преобразование для документов, содержащих 10 тысяч слов.Ниже мой текущий код.
<!-- 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>
Кто-нибудь знает более эффективный способ сделать это?
Спасибо.