使用XSLT 1.0版进行节点分组
我需要一些关于我正在编写的xslt的帮助。下面是我的源xml使用XSLT 1.0版进行节点分组,xslt,xslt-1.0,Xslt,Xslt 1.0,我需要一些关于我正在编写的xslt的帮助。下面是我的源xml <document> <content1></content1> <content2></content2> <Br/> <content3></content3> <content4></content4> <Br/> <content5&g
<document>
<content1></content1>
<content2></content2>
<Br/>
<content3></content3>
<content4></content4>
<Br/>
<content5></content5>
<content6></content6>
</document>
以下是我打算创建的输出结构:
<document>
<p>
<content1></content1>
<content2></content2>
</p>
<p>
<content3></content3>
<content4></content4>
</p>
<p>
<content5></content5>
<content6></content6>
</p>
</document>
我的问题是,每当我看到“”标记时,如何将内容分组并将其包装在“”标记中
谢谢 使用Muenchian方法将
文档
的儿童按其前一个Br
分组:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="byPosition" match="/document/*[not(self::Br)]"
use="generate-id(preceding-sibling::Br[1])"/>
<xsl:template match="@*|node()" name="identity" priority="-5">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/document/*[not(self::Br) and
generate-id()=generate-id(key('byPosition',
generate-id(preceding-sibling::Br[1]))[1])]">
<p><xsl:copy-of
select="key('byPosition',
generate-id(preceding-sibling::Br[1]))"/></p>
</xsl:template>
<xsl:template match="/document/*" priority="-3"/>
</xsl:stylesheet>
标识转换用于通过以下方式复制大多数节点:
<xsl:template match="@*|node()" name="identity" priority="-5">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
…并将其用作复制按该键分组的所有项目的点:
<xsl:template match="document/*[not(self::Br) and
generate-id()=generate-id(key('byPosition',
generate-id(preceding-sibling::Br[1]))[1])]">
<xsl:copy-of select="key('byPosition',
generate-id(preceding-sibling::Br[1]))"/>
此XSLT 1.0提供了预期结果:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="parag" match="document/*[not(self::Br)]" use="count(following-sibling::Br)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="document/*[not(self::Br) and not(position()=1)]"/>
<xsl:template match="Br|document/*[position()=1]">
<p>
<xsl:for-each select="key('parag',count(following-sibling::Br))">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:for-each>
</p>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="parag" match="document/*[not(self::Br)]" use="count(following-sibling::Br)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="document/*[not(self::Br) and not(position()=1)]"/>
<xsl:template match="Br|document/*[position()=1]">
<p>
<xsl:for-each select="key('parag',count(following-sibling::Br))">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:for-each>
</p>
</xsl:template>
</xsl:stylesheet>