高分请教xsl应用

snaill 2001-10-27 08:54:19
请问如何将以下a.xml通过xsl转换为b.xml?
a.xml
<man>
<name>John</name>
<address>addr1</address>
</man>
<man>
<name>Peter</name>
<address>addr2</address>
</man>

b.xml
<addr1>
<man>
<name>John</name>
<address>addr1</address>
</man>
</addr1>
<addr2>
<man>
<name>Peter</name>
<address>addr2</address>
</man>
</addr2>
在此先谢了

...全文
99 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
snaill 2001-11-08
thank you
  • 打赏
  • 举报
回复
karma 2001-11-07
assume you have
1.
<people>
<man>
<name>John</name>
<address>addr1</address>
</man>
<man>
<name>Peter</name>
<address>addr2</address>
</man>
<man>
<name>Peter2</name>
<address>addr1</address>
</man>
</people>

and you want
2
<people>
<addr1><man>
<name>John</name>
<address>addr1</address>
</man>
<man>
<name>Peter2</name>
<address>addr1</address>
</man>
</addr1>
<addr2>
<man>
<name>Peter</name>
<address>addr2</address>
</man>
</addr2>
</people>

use
3.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="man-addr" match="man" use="address" />

<xsl:template match="people">
<xsl:copy>
<xsl:for-each select = "man[count(. | key('man-addr',address)[1]) = 1]">
<xsl:element name="{address}">

<xsl:for-each select = "key('man-addr',address)">
<xsl:copy-of select="." />
</xsl:for-each>

</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
  • 打赏
  • 举报
回复
snaill 2001-11-07
to karma:
当有多个同地址的人时,转出的结果是:
<addr1>
<man>
<name>John</name>
<address>addr1</address>
</man>
</addr1>
<addr1>
<man>
<name>Peter</name>
<address>addr2</address>
</man>
</addr1>
能不能转成以下的形式?
<addr1>
<man>
<name>John</name>
<address>addr1</address>
</man>
<man>
<name>Peter</name>
<address>addr2</address>
</man>
</addr1>
  • 打赏
  • 举报
回复
osdx 2001-10-28
首先,xml文件有错误,xml文件中只能含有一个根!

如果假设b.xml文件的根为"root"
则相应的xsl文件应为:

<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
<xsl:output method = "xml" indent = "yes" />
<xsl:template match = "/" >
<root>
...
<addr1 >
<xsl:copy-of select = "//man[1]" />
</addr1>
<addr2 >
<xsl:copy-of select = "//man[2]" />
</addr2>
...
</root>
</xsl:template>
</xsl:stylesheet>

另外一种情况也可以:

<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
<xsl:output method = "xml" indent = "yes" />
<xsl:template match = "/" >
<xsl:element name = "root">
...
<xsl:element name = "addr1" >
<xsl:copy-of select = "//man[1]" />
</xsl:element>
<xsl:element name = "addr2" >
<xsl:copy-of select = "//man[2]" />
</xsl:element>
...
</xsl:element>
</xsl:template>
</xsl:stylesheet>
  • 打赏
  • 举报
回复
karma 2001-10-28
assume your xml is like this:
<people>
<man>
<name>John</name>
<address>addr1</address>
</man>
<man>
<name>Peter</name>
<address>addr2</address>
</man>
</people>

use this stylesheet (it should work with any number of <man>, as long your values of <address> can become valid element names):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<xsl:copy>
<xsl:for-each select = "man">
<xsl:element name="{address}">
<xsl:copy-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
  • 打赏
  • 举报
回复
发帖
XML/XSL
加入

8870

社区成员

XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
申请成为版主
帖子事件
创建了帖子
2001-10-27 08:54
社区公告
暂无公告