[XSLT难题]

CrazyJavar 2003-08-20 11:46:25
源xml文件如下,现在需要通过xslt转换得到一个新的xml文件,新xml中要把源xml中的<Dyna>元素中的x,y属性的值对调,其它部分保持和源xml一致
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="trans.xslt"?>
<source>
<title>XSL</title>
<author>John Smith</author>
<Dyna x="123" y="456">V1.0.0.0</Dyna>
</source>

转换xslt文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="node()">
<xsl:choose>
<xsl:when test="name()='Dyna'">
<xsl:call-template name="DynaTemplate"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="DefaultTemplate"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="DynaTemplate">
<xsl:copy>
<xsl:variable name="temp">
<xsl:value-of select="@x"/>
</xsl:variable>
<xsl:attribute name="x"><xsl:value-of select="@y"/></xsl:attribute>
<xsl:attribute name="y"><xsl:value-of select="$temp"/></xsl:attribute>
</xsl:copy>
</xsl:template>
<xsl:template name="DefaultTemplate">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>


得到的xml文件如下,没有达到预期的结果:
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="trans.xslt"?>

<source/>

请高手指点一下,该如何修改xslt以达到预期的结果呢,谢谢
...全文
38 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2003-08-20
  • 打赏
  • 举报
回复
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="Dyna">
<xsl:copy>
<xsl:attribute name="x"><xsl:value-of select="@y"/></xsl:attribute>
<xsl:attribute name="y"><xsl:value-of select="@x"/></xsl:attribute>
<xsl:copy-of select="@*[local-name() != 'x' and local-name() != 'y']"/>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>


<xsl:template match="node()">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
月光易水 2003-08-20
  • 打赏
  • 举报
回复
1. 路径错误
从文档根节点开始读取数据,只读取了(根节点)子节点, xml-stylesheet和source

2. 使用 xsl:cpoy-of ,而非xsl:copy
xsl:copy Attributes and children are not copied automatically


:_)
CrazyJavar 2003-08-20
  • 打赏
  • 举报
回复
谢谢楼上大虾指点,你的文件可以的

不过您能讲讲我写的错在哪里么?

谢谢
CrazyJavar 2003-08-20
  • 打赏
  • 举报
回复
谢谢楼上大虾指点

不过您能否讲讲我写的错在哪里么?

谢谢

8,906

社区成员

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

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