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

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

问题描述

我需要将 xml 属性中的值通过 xsl 转换为 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 变量中.

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 还是标记化,请注意这需要直接在浏览器中加载.

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天全站免登陆