求XSLT中最简单的给XML中所有结点加上数字唯一标识符的方法

herowach 2007-11-23 03:01:39
任意一个XML,想给它的每个结点都加上一个不重复的唯一的数字标识符(正整数),该如何实现?
有最简单的实现自增变量的方法吗?
...全文
140 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cds27 2007-12-03
  • 打赏
  • 举报
回复
你这个问题应该说属于遍历节点,可以参考在这篇:
http://blog.csdn.net/cds27/archive/2005/10/24/514457.aspx

稍做修改就可以了。
hejunbin 2007-11-30
  • 打赏
  • 举报
回复
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<ItemAdd>
<xsl:apply-templates select="root/item"/>
</ItemAdd>
</xsl:template>

<xsl:template match="item" >
<Item>
<xsl:attribute name="PosX">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:attribute name="PosY">
<xsl:value-of select="position()"/>
</xsl:attribute>
</Item>
<xsl:for-each select="item">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


这样可以不?。。。没有测试过,下次过来看看O.o
顺便帮你顶顶啊
herowach 2007-11-30
  • 打赏
  • 举报
回复
最后一次顶起~~~~~~~
herowach 2007-11-29
  • 打赏
  • 举报
回复
难道给每个结点加个自增编号真的这么难吗?
herowach 2007-11-26
  • 打赏
  • 举报
回复
顶起,虽然XSLT实现自增很麻烦,但为了风格统一,实在不想放在JAVA里去转换.
cds27 2007-11-24
  • 打赏
  • 举报
回复
很容易啊,给你一个语句,其他不难,你自己写吧.

这是绝对序号:<xsl:value-of select="count(preceding::*)"/>
herowach 2007-11-24
  • 打赏
  • 举报
回复
这是取结点之前的数量,但遇到树型就不行了啊,我的目标XML是一个树;
例如:

<?xml version="1.0" encoding="UTF-8" ?>
<root id="1">
<item id="2">
<item id="3">
<item id="4"/>
<item id="5"/>
</item>
<item id="6">
<item id="7"/>
</item>
<item id="8"/>
<item id="9"/>
<item id="10" />
</item>
</root>

XSLT是:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<ItemAdd>
<xsl:apply-templates select="root/item"/>
</ItemAdd>
</xsl:template>

<xsl:template match="item" >
<Item>
<xsl:attribute name="PosX">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:attribute name="PosY">
<xsl:value-of select="count(preceding::*)"/>
</xsl:attribute>
</Item>
<xsl:for-each select="item">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

而结果却是:

<ItemAdd>
<Item PosX="2" PosY="0" />
<Item PosX="3" PosY="0" />
<Item PosX="4" PosY="0" />
<Item PosX="5" PosY="1" />
<Item PosX="6" PosY="3" />
<Item PosX="7" PosY="3" />
<Item PosX="8" PosY="5" />
<Item PosX="9" PosY="6" />
<Item PosX="10" PosY="7" />
</ItemAdd>

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧