Xslt 相邻元素相同的xsl编号
我需要用以下结构格式化一些xml数据Xslt 相邻元素相同的xsl编号,xslt,xpath,Xslt,Xpath,我需要用以下结构格式化一些xml数据 <list> <item> Test </item> <item> Testt </item> <or-item> TestOr </or-item> <or-item> TestOrr </or-item> <item> Testtt </item>
<list>
<item>
Test
</item>
<item>
Testt
</item>
<or-item>
TestOr
</or-item>
<or-item>
TestOrr
</or-item>
<item>
Testtt
</item>
<or-item>
TestOrrr
</or-item>
<item>
Testttt
</item>
</list>
编辑 我在linux上使用XSLT1.1和xsltproc,但如果必要,可以使用2.0
由于目标格式是HTML,您似乎可以依靠使用
xsl:for each group
和group start with=“item”
来创建适当的嵌套HTML有序列表:
由于目标格式是HTML,您似乎可以依靠使用
xsl:for each group
和group start with=“item”
来创建适当的嵌套HTML有序列表:
只需调整
xsl:number指令即可生成预期的输出:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/list">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="item">
<div>
<xsl:number/>
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="or-item">
<div style="padding-left: 10px">
<xsl:number level="any" from="item" format="a) "/>
<xsl:value-of select="."/>
</div>
</xsl:template>
</xsl:stylesheet>
只需调整xsl:number指令
:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/list">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="item">
<div>
<xsl:number/>
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="or-item">
<div style="padding-left: 10px">
<xsl:number level="any" from="item" format="a) "/>
<xsl:value-of select="."/>
</div>
</xsl:template>
</xsl:stylesheet>
能否向我们展示您希望为所展示的XML示例生成的结果?您使用哪个XSLT处理器,哪个XSLT版本I1.1从未真正作为规范存在过?您能告诉我们您想要为您展示的XML示例生成的结果吗?您使用哪个XSLT处理器,哪个XSLT版本I1.1从未真正作为规范存在过?嗯saxon
知道tail
函数,但是Altova
中存在问题。。。有什么替代品吗?@Pascal,是的,tail(current-group())
本质上是current-group()[position()gt 1]
或子序列(current-group(),2)
。嗯saxon
知道tail
函数,但Altova
中存在问题。。。有什么替代品吗?@Pascal,是的,tail(current-group())
本质上是current-group()[position()gt 1]
或子序列(current-group(),2)
。
<xsl:template match="list">
<xsl:variable name="nested-list">
<xsl:for-each-group select="*" group-starting-with="item">
<xsl:copy>
<xsl:value-of select="."/>
<xsl:copy-of select="tail(current-group())"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:variable>
<div>
<xsl:apply-templates select="$nested-list"/>
</div>
</xsl:template>
<xsl:template match="item">
<div>
<xsl:number/>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="or-item">
<div style="padding-left: 10px">
<xsl:number format="a) "/>
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/list">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="item">
<div>
<xsl:number/>
<xsl:value-of select="."/>
</div>
</xsl:template>
<xsl:template match="or-item">
<div style="padding-left: 10px">
<xsl:number level="any" from="item" format="a) "/>
<xsl:value-of select="."/>
</div>
</xsl:template>
</xsl:stylesheet>