XSLT - 如何在不修改变量的情况下保存要添加的信息?

时间:2016-04-20 23:26:02

标签: xml xslt

我正在尝试编写一个XSLT文档,该文档将我之前从MEI XML文档中提取的信息转换为SVG图。我遇到的问题是,有时在特定值中有多个元素需要在将它们添加到一起之前进行转换。我的第一个冲动是使用一系列if语句并将结果一起添加,但XSLT变量无法修改。

以下是我的XML代码片段:

<song>
   <verb>
      <word>Close</word>
      <connotation>negative</connotation>
       <duration>4</duration>
    </verb>
       <verb>
      <word>Hide</word>
      <connotation>negative</connotation>
      <duration>4</duration>
   </verb>
   <verb>
      <word>Bar</word>
      <connotation>negative</connotation>
      <duration>4</duration>
   </verb>
 </song>

这是我的XSLT:

                   

<xsl:template match="/">


    <svg height="100%" width="100%">
        <g transform="translate(50,220)">

            <line x1="0" y1="0" x2="{count(//verb) * 21}" y2="0" stroke="black" stroke-width="2"/>
            <line x1="0" y1="-200" x2="0" y2="200" stroke="black" stroke-width="2"/>

            <line x1="-4" x2="4" y1="20" y2="20" stroke="black" stroke-width="1"/>
            <text x="-12" y="25">2</text>

            <line x1="-4" x2="4" y1="40" y2="40" stroke="black" stroke-width="1"/>
            <text x="-12" y="45">4</text>

            <line x1="-4" x2="4" y1="60" y2="60" stroke="black" stroke-width="1"/>
            <text x="-12" y="65">6</text>

            <line x1="-4" x2="4" y1="80" y2="80" stroke="black" stroke-width="1"/>
            <text x="-12" y="85">8</text>

            <line x1="-4" x2="4" y1="100" y2="100" stroke="black" stroke-width="1"/>
            <text x="-21" y="105">10</text>

            <line x1="-4" x2="4" y1="120" y2="120" stroke="black" stroke-width="1"/>
            <text x="-21" y="125">12</text>

            <line x1="-4" x2="4" y1="140" y2="140" stroke="black" stroke-width="1"/>
            <text x="-21" y="145">14</text>

            <line x1="-4" x2="4" y1="160" y2="160" stroke="black" stroke-width="1"/>
            <text x="-21" y="165">16</text>

            <line x1="-4" x2="4" y1="180" y2="180" stroke="black" stroke-width="1"/>
            <text x="-21" y="185">18</text>

            <line x1="-4" x2="4" y1="200" y2="200" stroke="black" stroke-width="1"/>
            <text x="-21" y="205">20</text>

            <line x1="-4" x2="4" y1="-20" y2="-20" stroke="black" stroke-width="1"/>
            <text x="-12" y="-15">2</text>

            <line x1="-4" x2="4" y1="-40" y2="-40" stroke="black" stroke-width="1"/>
            <text x="-12" y="-35">4</text>

            <line x1="-4" x2="4" y1="-60" y2="-60" stroke="black" stroke-width="1"/>
            <text x="-12" y="-55">6</text>

            <line x1="-4" x2="4" y1="-80" y2="-80" stroke="black" stroke-width="1"/>
            <text x="-12" y="-75">8</text>

            <line x1="-4" x2="4" y1="-100" y2="-100" stroke="black" stroke-width="1"/>
            <text x="-21" y="-95">10</text>

            <line x1="-4" x2="4" y1="-120" y2="-120" stroke="black" stroke-width="1"/>
            <text x="-21" y="-115">12</text>

            <line x1="-4" x2="4" y1="-140" y2="-140" stroke="black" stroke-width="1"/>
            <text x="-21" y="-135">14</text>

            <line x1="-4" x2="4" y1="-160" y2="-160" stroke="black" stroke-width="1"/>
            <text x="-21" y="-155">16</text>

            <line x1="-4" x2="4" y1="-180" y2="-180" stroke="black" stroke-width="1"/>
            <text x="-21" y="-175">18</text>

            <line x1="-4" x2="4" y1="-200" y2="-200" stroke="black" stroke-width="1"/>
            <text x="-21" y="-195">20</text>

            <xsl:apply-templates select="//verb"/>
        </g>
    </svg>
</xsl:template>
<xsl:template match="verb">
    <xsl:variable name="verbpos" select="position() - 1"/>
    <xsl:variable name="xposition" select="$verbpos * $barInterval"/>
    <xsl:variable name="barHeight" select="duration * 10"/><!-- remember to multiply by 10 later -->



  <!-- if test each duration within verbs
      if = to a specific note, assign actualValue


      <xsl:if test="duration = 4">
   <xsl:variable name="acutalValue" select="1"/>
    </xsl:if>-->


    <xsl:if test="connotation = 'positive'">
        <rect x="{$xposition + $barShift}" y="-{$barHeight}" stroke="black" stroke-width=".5"
            fill="#e2727b" width="{$barWidth}" height="{$barHeight}" class="{lower-case(word)}"/>
    </xsl:if>
    <xsl:if test="connotation = 'neutral'">
        <rect x="{$xposition + $barShift}" y="-{$barHeight div 2}" stroke="black"
            stroke-width=".5" fill="#bcdce5" width="{$barWidth}" height="{$barHeight}"
            class="{lower-case(word)}"/>
    </xsl:if>
    <xsl:if test="connotation = 'negative'">
        <rect x="{$xposition + $barShift}" y="0" stroke="black" stroke-width=".5" fill="#5c305c"
            width="{$barWidth}" height="{$barHeight}" class="{lower-case(word)}"/>
    </xsl:if>
</xsl:template>

我应该使用哪种数据结构进行此转换?或者有更简单的方法吗?先感谢您!

1 个答案:

答案 0 :(得分:0)

变量不一定包含单个值,实际上您可以在变量中包含整个文档,甚至可以在变量声明中声明仅在声明范围内的临时变量。

例如:

<xsl:variable name="myVar">
  <xsl:variable name="temp">
    <xsl:apply-templates select="childNodes"/>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$parameter = 'value'">
      <xsl:text>myVar contents</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>There are </xsl:text>
      <xsl:value-of select="count($temp//*)"/>
      <xsl:text> elements in temp</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

关键是,你可以在变量声明中包含所有逻辑,包括任何条件。无论模板可以输出什么,变量都可以存储。对不起,我没有提供具体的帮助,但我不清楚你的要求,但我希望这种技术能指出你正确的方向。