【发布时间】:2013-07-24 15:25:33
【问题描述】:
我有以下 XML,该 XML 中有多次出现的“item”元素,并且会有单次出现的“info”元素
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://temo.com/tempe.xsd">
<di>
<md>2013-07-09T09:43:00</md>
</di>
<list>
<item>
<Name>test</Name>
<section block1="true">
<block1>
<move>1</move>
<info>
<item1>test item 1</item1>
<item2>false</item2>
<item3>1</item3>
</info>
</block1>
<block2>
...
</block2>
</section>
</item>
</list>
<option>
...
</option>
</root>
我想将其转换为以下格式,即,如果存在“移动”元素,则应在“信息”元素的最后位置添加新元素
<item4>
<item5>1</item5>
</item4>
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://temo.com/tempe.xsd">
<di>
<md>2013-07-09T09:43:00</md>
</di>
<list>
<item>
<Name>test</Name>
<section block1="true">
<block1>
<move>1</move>
<info>
<item1>test item 1</item1>
<item2>false</item2>
<item3>1</item3>
<item4>
<item5>1</item5>
</item4>
</info>
</block1>
<block2>
...
</block2>
</section>
</item>
<item>
...
</item>
</list>
<option>
...
</option>
</root>
我正在使用下面的 XSLT 来转换成上面的格式
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/info/*[position()=last()]">
<xsl:copy>
<xsl:choose>
<xsl:when test="/section/block/move">
<!--Add new element item4-->
<xsl:element name="item4">
<xsl:element name="item5">
<xsl:value-of select="section/block/move"/>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="identity" />
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
您能帮我找出 XSLT 中的问题吗? 我是 XSLT 的新手
谢谢你:)
【问题讨论】: