使用XSL将具有2个或更多值的XML属性转换为SVG x/y坐标 [英] Turn XML attribute with 2 or more values into SVG x/y coordinates using XSL

查看:114
本文介绍了使用XSL将具有2个或更多值的XML属性转换为SVG x/y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过xsl将xml属性中的值转换为svg rect pos x和y

I need to turn the values in a xml attribute through xsl into a svg rect pos x and y

因此需要从中解析

示例

<item value="20 10 40" />
<item value="200 0 100" />
<item value="2666 10 40 95" />

按照一些规则解析每个项目值属性(那是我不确定的部分,如何将数字提取到单独的变量中)

parse each item value attribute following some rules (that's the part I'm not sure about, how to extract the numbers into separate variables)

喜欢

 <item value="20 10 40" />
              X  Z  Y

提取var1中的X(20)和var2中的Y(40)

Extract X (20) in var1 and Y (40) in var2

进入

<rect x="{$var1}" y="{$var2}" />

(如果在这种情况下需要第一个和第三个值)

(if I want the first and third value in this case)

基本上,我需要了解如何解析属性VALUE中包含的一系列多个值中的任何两个,并将它们分别作为var1和var2传递到rect vars中.

Basically I need to understand how to parse any TWO of a series of multiple values contained within the attribute VALUE and pass them into the rect vars as var1 and var2 separately.

到目前为止,我的研究发现了2种方法,但不确定在这种情况下如何应用,无论是substring-before和after还是tokenize,请注意,这需要直接在浏览器中加载.

From my research so far, I have found out 2 methods but not sure how to apply in this case, either substring-before and after or tokenize, please note this needs to work loaded directly in a browser.

编辑1

到目前为止,我已经找到一个丑陋的解决方案来提取3个数字的情况下的数据

I have found an ugly solution so far to extract the data for a case of 3 numbers

Xml

<item value="20 10 40" />

Xsl

Value 1    <xsl:value-of select="substring-before (@value, ' ')"/>
Value 2    <xsl:value-of select="substring-before(substring-after (@value, ' '), ' ')"/>
Value 3    <xsl:value-of select="substring-after(substring-after (@value, ' '), ' ')"/>

结果

Value 1    20
Value 2    10
Value 3    40

因此,正在寻找一种更简洁的方法,也许是递归方法,可以接受字符串中任意数量的数字并解析所有内容.

So looking for something cleaner, perhaps recursive that accept any amount of numbers in the string and parse all.

推荐答案

从XSLT 1.0的定界列表中提取值很麻烦,因为它没有tokenize()函数.

Extracting values from a delimited list in XSLT 1.0 is awkward, since it has no tokenize() function.

如果列表中的值数量很少(例如您的示例中的内容),则可以使用嵌套的substring-before()substring-after()调用,如您的问题所示(现在).

If the number of values in the list is small (like in your example), you can use nested substring-before() and substring-after() calls, as shown (now) in your question.

更通用的解决方案(也更适合处理更大的列表)将使用递归命名模板,例如:

A more generic solution, which is also more suitable to handle larger lists, would use a recursive named template, for example:

<xsl:template name="get-Nth-value">
    <xsl:param name="list"/>
    <xsl:param name="N"/>
    <xsl:param name="delimiter" select="' '"/>
    <xsl:choose>
        <xsl:when test="$N = 1">
            <xsl:value-of select="substring-before(concat($list, $delimiter), $delimiter)"/>
        </xsl:when>
        <xsl:when test="contains($list, $delimiter) and $N > 1">
            <!-- recursive call -->
            <xsl:call-template name="get-Nth-value">
                <xsl:with-param name="list" select="substring-after($list, $delimiter)"/>
                <xsl:with-param name="N" select="$N - 1"/>
                <xsl:with-param name="delimiter" select="$delimiter"/>
            </xsl:call-template>
        </xsl:when>
    </xsl:choose>
</xsl:template>


通话示例:


Example of call:

<xsl:template match="item">
    <rect>
        <xsl:attribute name="x">
            <xsl:call-template name="get-Nth-value">
                <xsl:with-param name="list" select="@value"/>
                <xsl:with-param name="N" select="1"/>
            </xsl:call-template>                
        </xsl:attribute>
        <xsl:attribute name="y">
            <xsl:call-template name="get-Nth-value">
                <xsl:with-param name="list" select="@value"/>
                <xsl:with-param name="N" select="2"/>
            </xsl:call-template>                
        </xsl:attribute>
    </rect>
</xsl:template>

演示: http://xsltransform.net/ncdD7mh

这篇关于使用XSL将具有2个或更多值的XML属性转换为SVG x/y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆