【发布时间】: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 结果吗?您的描述和代码不是很直观。