次の回答は@stwisselほどエレガントではありませんが、段落内のサブツリーを正しくタグ付けします。確かに、それは少し厄介になりました。:-)
このタスクの問題点は、終了タグとそれに続く対応する開始タグ (例: <tag></tag>
) の間にあるものを特別に処理する必要があることです。ただし、XSLT は、開始タグとそれに対応する終了タグ (例: ) の間にあるものを処理するために最適化されてい</tag><tag>
ます。ところで、少し「ごまかす」方法があります。この質問に対する私の他の回答を参照してください。
次のような入力 XML があるとします。
<pages>
<page>
This is a paragraph on the page.
<B>bold</B>
After Bold
<newParagraph/>
This is another paragraph.
<newParagraph/>
Here is yet another paragraph on this page.
<EM>
<B>
Bold and emphasized.
</B>
</EM>
After bold and emphasized.
</page>
<page>
Another page.
</page>
</pages>
この XSLT 1.0 変換を使用して処理できます
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="page">
<page>
<!-- handle the first paragraph up to the first newParagraph -->
<P>
<xsl:apply-templates select="node()[not(preceding-sibling::newParagraph)]" />
</P>
<!-- now handle all remaining paragraphs of the page -->
<xsl:for-each select="newParagraph">
<xsl:variable name="pCount" select="position()"/>
<P>
<xsl:apply-templates select="following-sibling::node()[count(preceding-sibling::newParagraph) <= $pCount]" />
</P>
</xsl:for-each>
</page>
</xsl:template>
<!-- this default rule recursively copies all substructures within a paragraph at tag level -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- this default rule makes sure that texts between the tags are printed -->
<xsl:template match="text()">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="newParagraph"/>
</xsl:stylesheet>
この出力を生成する
<pages>
<page><P>
This is a paragraph on the page.
<B>bold</B>
After Bold
</P><P>
This is another paragraph.
</P><P>
Here is yet another paragraph on this page.
<EM>
<B>
Bold and emphasized.
</B>
</EM>
After bold and emphasized.
</P></page>
<page><P>
Another page.
</P></page>
</pages>