【问题标题】:XSLT - How do I hold information to be added together without modifying a variable?XSLT - 如何在不修改变量的情况下保存要添加的信息?
【发布时间】:2016-04-21 21:05:06
【问题描述】:

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

这是我的 XML 代码的 sn-p:

<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>

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

【问题讨论】:

  • 你能从示例 sn-p XML 中发布所需的 XML 结果吗?您的描述和代码不是很直观。

标签: xml xslt


【解决方案1】:

一个变量不一定只有一个值,事实上你可以在一个变量中包含整个文档,你甚至可以在一个变量声明中声明临时变量,这些变量只在声明的范围内。

例如:

<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>

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

【讨论】:

    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2011-11-03
    • 2016-08-13
    相关资源
    最近更新 更多