Пожалуйста, предложите, как сгруппировать текстовый узел и некоторые элементы, такие как 'i
' или 'b
' или 'list
' внутри 'p
' элемент. Обеспечение div
не должно быть равно p
.Группировка текстового узла и смежных элементов определенного типа
XML: (с разрывами строк или пробелы для отображения цели, для запуска использовать ниже 2-го XML)
<article>
<body>
<para>
<display><fig>Fig1</fig></display>
the text node1
</para>
<para>
<display><fig>Fig1</fig></display>
</para>
<para>
<display><fig>Fig1</fig></display>
the text node1 <i>h</i> ther <b>b</b> the text4
<display><tab>Table1</tab></display>
the text node2
<list><li>list1</li></list>
</para>
<para>The text node3</para>
</body>
</article>
XML: (без разрывов строк)
<article><body><para><display><fig>Fig1</fig></display>the text node1</para><para><display><fig>Fig1</fig></display></para><para><display><fig>Fig1</fig></display>the text node1 <i>h</i> ther <b>b</b> the text4<display><tab>Table1</tab></display>the text node2<list><li>list1</li></list></para><para>The text node3</para></body></article>
XSLT :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="para">
<xsl:choose>
<xsl:when test="not(text())"><xsl:apply-templates/></xsl:when>
<xsl:when test="display and text() or *">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="name()='display'"><div><xsl:apply-templates/></div></xsl:when>
<xsl:when test="name()='i' or name()='b'">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:when>
<xsl:when test="not(*)"><p><xsl:value-of select="."/></p></xsl:when><!--Here grouping required with adjacent elements 'i' or 'b' etc -->
<xsl:otherwise><p><xsl:apply-templates/></p></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<p><xsl:apply-templates/></p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Обязательный Результат:
<article>
<body>
<div><fig>Fig1</fig></div><!--ensure div should not child to 'p'-->
<p>the text node1</p> <!--Text area including 'i' and 'b' to be within 'p' -->
<div><fig>Fig1</fig></div>
<div><fig>Fig1</fig></div>
<p>the text node1 <i>h</i> ther <b>b</b> the text4</p><!--Text area including 'i' and 'b' to be within 'p' -->
<div><tab>Table1</tab></div>
<p>the text node2<list><li>list1</li></list></p><!--text area includes 'list' element -->
<p>The text node3</p>
</body>
</article>
Большое спасибо за хорошее предложение, плюс одно. –