+ -
当前位置:首页 → 问答吧 → xsl求助高人,再次洗牌。。。。

xsl求助高人,再次洗牌。。。。

时间:2011-04-17

来源:互联网

其实这个题目有发过,但没有合适的,想再发一次,再次求助高人。
题目要求是这样的,name 的个数大于等于小于note的情况都要考虑的,所以,,,,
我自己的代码写的很长很复杂,求助高人。。

下面是input:
XML code

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE persons SYSTEM "persons.dtd">

<persons>
  <names>
    <name>Alan Turing</name>
    <name>Kurt Gödel</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>

输出:
XML Source Code
<?xml version="1.0" encoding="UTF-8"?>
<persons>
   <name>Alan Turing</name>
   <note>Defined a simple theoretical model of computers</note>
   <name>Kurt Gödel</name>
   <note>Proved the incompleteness of arithmetics</note>
   <note>Prolific author and creator of TeX</note>
   <note>Proposed a model of concurrency</note>
</persons>

下面是我的xsl,献丑了!大家帮我简化简化哈。。。
<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform version="2.0"
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="shu">
          <xsl:with-param name ="seq1" select="names/name" as="element(name)*"/>
          <xsl:with-param name="seq2" select ="notes/note" as="element(note)*"/>
        </xsl:call-template>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="shu">
    <xsl:param name="seq1" as="element(name)*"/>
    <xsl:param name="seq2" as="element(note)*"/>
    <xsl:choose>
      <xsl:when test="(empty($seq1)) and (empty($seq2))"/>
      <xsl:when test="not(empty($seq1))">
        <xsl:sequence select="$seq1[1]"/>
        <xsl:sequence select="$seq2[1]"/>
        <xsl:call-template name="shu">
          <xsl:with-param name="seq1" select="$seq1[position()>1]"/>
          <xsl:with-param name="seq2" select="$seq2[position()>1]"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="$seq2[1]"/>
        <xsl:call-template name="shu">
          <xsl:with-param name="seq2" select="$seq2[position()>1]"/>
          <xsl:with-param name="seq1" select="()"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:transform>


作者: cqq8638   发布时间: 2011-04-17

依旧没有人理我,自己顶一个!

作者: cqq8638   发布时间: 2011-04-17