使用XSL将具有2个或更多值的XML属性转换为SVG x / y坐标
我需要通过xsl将xml属性中的值转换为svg rect pos x和y
因此需要解析 例子 <item value="20 10 40" /> <item value="200 0 100" /> <item value="2666 10 40 95" /> 按照一些规则解析每个项值属性(这是我不确定的部分,如何将数字提取到单独的变量中) 喜欢 <item value="20 10 40" /> X Z Y 在var1中提取X(20),在var2中提取Y(40) 成 <rect x="{$var1}" y="{$var2}" /> (如果我想要这种情况下的第一个和第三个值) 基本上我需要了解如何解析属性VALUE中包含的一系列多个值中的任何两个值,并将它们分别作为var1和var2传递到rect变量中. 从我到目前为止的研究中,我发现了2种方法,但不知道如何在这种情况下应用,无论是substring-before还是after或tokenize,请注意这需要直接在浏览器中加载. EDIT1 到目前为止,我找到了一个丑陋的解决方案来提取3个数字的数据 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 所以寻找更清洁的东西,也许是递归的,接受字符串中的任意数量的数字并解析所有.
从XSLT 1.0中的分隔列表中提取值很尴尬,因为它没有tokenize()函数.
如果列表中的值的数量很小(如示例中所示),则可以使用嵌套的substring-before()和substring-after()调用,如问题中所示(现在). 更通用的解决方案,也更适合处理更大的列表,将使用递归命名模板,例如: <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> 通话示例: <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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |