洗牌序列输出问题,xsl,求助高手啊!!!!!
input:
<!DOCTYPE persons SYSTEM "persons.dtd">
<persons>
<names>
<name>Alan Turing</name>
<name>Kurt Gödel</name>
<name>Donald Knuth</name>
<name>Robin Milner</name>
</names>
<notes>
<note>Defined a simple theoretical model of computers</note>
<note>Proved the incompleteness of arithmetics</note>
<note>Prolific author and creator of TeX</note>
<note>Proposed a model of concurrency</note>
</notes>
</persons>
output:(这个是要求输出的内容)
XML Source Code
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<name>Alan Turing</name>
<note>Defined a simple theoretical model of computer </note>
<name>Kurt Gödel</name>
<note>Proved the incompleteness of arithmetics</note>
<name>Donald Knuth</name>
<note>Prolific author and creator of TeX</note>
<name>Robin Milner</name>
<note>Proposed a model of concurrency</note>
</persons>
一下是我的代码:大家帮忙看下啊哈:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent ="yes"/>
<xsl:template match="persons">
<xsl:copy>
<xsl:call-template name="shuffle">
<xsl:with-param name ="seq" select="names/name" as="element(name)*"/>
</xsl:call-template>
<xsl:call-template name="shuffle2">
<xsl:with-param name ="seq2" select="notes/note" as="element(note)*"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="shuffle">
<xsl:param name="seq" as="element(name)*"/>
<xsl:sequence select="$seq[1]"/>
<xsl:if test="not(empty($seq))">
<xsl:call-template name="shuffle">
<xsl:with-param name="seq" select="$seq[position()>1]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="shuffle2">
<xsl:param name="seq2" as="element(node)*"/>
<xsl:sequence select="$seq2[1]"/>
<xsl:if test="not(empty($seq2))">
<xsl:call-template name="shuffle2">
<xsl:with-param name="seq2" select="$seq2[position()>1]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:transform>
[解决办法]
都xsl2.0了啊
下面的xsl1.0的可以简单实现
- XML code
<?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" version="1.0" encoding="UTF-8" indent ="yes"/> <xsl:template match="persons"> <xsl:copy> <xsl:for-each select="names/name"> <xsl:copy> <xsl:value-of select="."/> </xsl:copy> <xsl:call-template name="note"> <xsl:with-param name="index" select="position()"></xsl:with-param> </xsl:call-template> </xsl:for-each> </xsl:copy> </xsl:template> <xsl:template name="note"> <xsl:param name="index"></xsl:param> <xsl:for-each select="/persons/notes/note[position()=$index]"> <xsl:copy> <xsl:value-of select="."/> </xsl:copy> </xsl:for-each> </xsl:template></xsl:stylesheet>