移动分离器元件向上以XML的层次结构
题
我有隔板的xml文档在分层结构深处。
<A>
<B>
<C id='1'/>
<separator/>
<C id='2'/>
</B>
<B>
<C id='3'/>
<separator/>
</B>
<B>
<C id='4'/>
</B>
</A>
我想向上移动隔板,保持元件,以便。所以期望的输出是
<A>
<B>
<C id='1'/>
</B>
</A>
<separator/>
<A>
<B>
<C id='2'/>
</B>
<B>
<C id='3'/>
</B>
</A>
<separator/>
<A>
<B>
<C id='4'/>
</B>
</A>
怎样才可以使用XSLT仅1.0做什么?能不能不用for-each
完成,使用模板匹配而已?
更新: 事实上,我不同层次的一般性的4分辉煌的答案,谢谢你,伙计们。
解决方案
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kCByFollSep" match="C"
use="generate-id(following::separator[1])"/>
<xsl:template match="A">
<xsl:for-each select="B/separator|B[last()]/*[last()]">
<A>
<xsl:apply-templates
select="key('kCByFollSep',
substring(generate-id(),
1 div boolean(self::separator)))"/>
</A>
<xsl:copy-of select="self::separator"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="C">
<B>
<xsl:copy-of select="."/>
</B>
</xsl:template>
</xsl:stylesheet>
输出:
<A>
<B>
<C id="1" />
</B>
</A>
<separator />
<A>
<B>
<C id="2" />
</B>
<B>
<C id="3" />
</B>
</A>
<separator />
<A>
<B>
<C id="4" />
</B>
</A>
注意强>:由以下separator
,添加了对更多钞票C
最后第三级元件,而不必遵循separator
分组
修改强>:更多拉式,更多的模式无关,此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kCByFollSep" match="C"
use="generate-id(following::separator[1])"/>
<xsl:template match="text()"/>
<xsl:template match="separator|*[not(*)][not(following::*)]">
<A>
<xsl:apply-templates
select="key('kCByFollSep',
substring(generate-id(),
1 div boolean(self::separator)))"
mode="output"/>
</A>
<xsl:copy-of select="self::separator"/>
</xsl:template>
<xsl:template match="C" mode="output">
<B>
<xsl:copy-of select="."/>
</B>
</xsl:template>
</xsl:stylesheet>
编辑2 :更多通用的解决方案(!有一两件事我不相信,JA)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*" name="identity">
<xsl:param name="pRemains"/>
<xsl:copy>
<xsl:apply-templates select="node()[descendant-or-self::node()
[not(self::separator)]
[count(following::separator)
= $pRemains]
][1]|@*">
<xsl:with-param name="pRemains" select="$pRemains"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()
[descendant-or-self::node()
[not(self::separator)]
[count(following::separator)
= $pRemains]
][1]">
<xsl:with-param name="pRemains" select="$pRemains"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="/*">
<xsl:variable name="vCurrent" select="."/>
<xsl:for-each select="descendant::separator|node()[last()]">
<xsl:variable name="vRemains" select="last()-position()"/>
<xsl:for-each select="$vCurrent">
<xsl:copy>
<xsl:apply-templates
select="node()[descendant::node()
[not(self::separator)]
[count(following::separator)
= $vRemains]
][1]">
<xsl:with-param name="pRemains" select="$vRemains"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:for-each>
<xsl:copy-of select="self::separator"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="separator"/>
</xsl:stylesheet>
注意强>:晴细粒遍历。复制本身和分离器(虚拟节点对最后一组,而不必遵循分离器)使剩余部分的隔板的地板层级规则(在这种情况下,根元素)来处理第一子以足够的下面的隔板来处理。一种改进的细粒穿越身份规则,复制自身,再处理的第一个孩子,并经与足以下分离工艺的兄弟姐妹。最后,隔板规则破坏过程。
修改3 强>:其它更一般的解决方案,现在用递归身份规则
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="kNodeByFolSep" match="node()[not(self::separator)]"
use="generate-id((descendant::separator|following::separator)[1])"/>
<xsl:template match="node()|@*" name="identity">
<xsl:param name="pGroup"/>
<xsl:copy>
<xsl:apply-templates
select="node()[descendant-or-self::node()[count(.|$pGroup)
= count($pGroup)]]|@*">
<xsl:with-param name="pGroup" select="$pGroup"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:variable name="vCurrent" select="."/>
<xsl:for-each select="descendant::separator|node()[last()]">
<xsl:variable name="vGroup"
select="key('kNodeByFolSep',generate-id(self::separator))"/>
<xsl:for-each select="$vCurrent">
<xsl:call-template name="identity">
<xsl:with-param name="pGroup" select="$vGroup"/>
</xsl:call-template>
</xsl:for-each>
<xsl:copy-of select="self::separator"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="separator"/>
</xsl:stylesheet>
修改4 强>:现在只是与以前相同,但与关键试验,而不是节点交集
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="kNodeByFolSep" match="node()[not(self::separator)]"
use="concat(generate-id(),'+',
generate-id((descendant::separator|
following::separator)[1]))"/>
<xsl:template match="node()|@*" name="identity">
<xsl:param name="pSeparator"/>
<xsl:copy>
<xsl:apply-templates
select="@*|node()[descendant-or-self::node()
[key('kNodeByFolSep',
concat(generate-id(),
'+',
$pSeparator))]]">
<xsl:with-param name="pSeparator" select="$pSeparator"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:variable name="vCurrent" select="."/>
<xsl:for-each select="descendant::separator|node()[last()]">
<xsl:variable name="vSeparator"
select="generate-id(self::separator)"/>
<xsl:for-each select="$vCurrent">
<xsl:call-template name="identity">
<xsl:with-param name="pSeparator" select="$vSeparator"/>
</xsl:call-template>
</xsl:for-each>
<xsl:copy-of select="self::separator"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="separator"/>
</xsl:stylesheet>
其他提示
:此转化强>:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kFollowing" match="C"
use="generate-id(preceding::separator[1])"/>
<xsl:template match="/">
<xsl:apply-templates select="*/*/separator"/>
</xsl:template>
<xsl:template match="separator" name="commonSep">
<separator/>
<xsl:call-template name="genAncestors">
<xsl:with-param name="pAncs" select="ancestor::*"/>
<xsl:with-param name="pLeaves"
select="key('kFollowing', generate-id())"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="separator[not(preceding::separator)]">
<xsl:call-template name="genAncestors">
<xsl:with-param name="pAncs" select="ancestor::*"/>
<xsl:with-param name="pLeaves"
select="key('kFollowing', '')"/>
</xsl:call-template>
<xsl:call-template name="commonSep"/>
</xsl:template>
<xsl:template name="genAncestors">
<xsl:param name="pAncs" select="ancestor::*"/>
<xsl:param name="pLeaves" select="."/>
<xsl:choose>
<xsl:when test="not($pAncs[2])">
<xsl:apply-templates select="$pLeaves" mode="gen"/>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$pAncs[1]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:call-template name="genAncestors">
<xsl:with-param name="pAncs" select="$pAncs[position()>1]"/>
<xsl:with-param name="pLeaves" select="$pLeaves"/>
</xsl:call-template>
</xsl:copy>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="C" mode="gen">
<xsl:variable name="vCur" select="."/>
<xsl:for-each select="..">
<xsl:copy>
<xsl:copy-of select="@*|$vCur"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
:当所提供的XML文档应用强>:
<A>
<B>
<C id='1'/>
<separator/>
<C id='2'/>
</B>
<B>
<C id='3'/>
<separator/>
</B>
<B>
<C id='4'/>
</B>
</A>
<强>产生想要的,正确的结果强>:
<A>
<B>
<C id="1"/>
</B>
</A>
<separator/>
<A>
<B>
<C id="2"/>
</B>
<B>
<C id="3"/>
</B>
</A>
<separator/>
<A>
<B>
<C id="4"/>
</B>
</A>
不隶属于 StackOverflow