动态增加属性值 XSLT [英] increment attribute value dynamically XSLT

查看:32
本文介绍了动态增加属性值 XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态设置属性,但我根本无法做到.你可以帮帮我吗?理想的例子:)

I'm trying to set attributes dynamically, but I can't do it at all. Could you help me? Desirable example :)

输入:

<root>
<row>
    <col>v11</col>
    <col>v12</col>
    <col>v13</col>
    <col>v14</col>
  </row>
  <row>
    <col>v21</col>
    <col>v22</col>
    <col>v23</col>
    <col>v24</col>
  </row>
</root>

当前的 XSLT 方案:

Current XSLT Scheme:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>
    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="row"/>
        </root>
    </xsl:template>

    <xsl:template match="col">
        <data col="1" row="1">
            <xsl:value-of select="."/>
        </data>
    </xsl:template>

    <xsl:template match="row">
        <xsl:apply-templates select="col"/>
    </xsl:template>

</xsl:stylesheet>

当前输出:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <data col="1" row="1">v11</data>
  <data col="1" row="1">v12</data>
  <data col="1" row="1">v13</data>
  <data col="1" row="1">v14</data>
  <data col="1" row="1">v21</data>
  <data col="1" row="1">v22</data>
  <data col="1" row="1">v23</data>
  <data col="1" row="1">v24</data>
</root>

如何动态分配属性值?

我需要这样做:

<root>
   <data row="1" col="1">v11</data>
   <data row="1" col="2">v12</data>
   <data row="1" col="3">v13</data>
   <data row="1" col="4">v14</data>
   <data row="2" col="1">v21</data>
   <data row="2" col="2">v22</data>
   <data row="2" col="3">v23</data>
   <data row="2" col="4">v24</data>
</root>

谢谢!

推荐答案

如果修改 apply-templatescol 元素传入 col 元素的位置代码>行...

If you amend the apply-templates for the col element to pass in the postion of the row...

<xsl:apply-templates select="col">
    <xsl:with-param name="row" select="position()" />
</xsl:apply-templates>

然后,在匹配row的模板中,你可以使用这个参数,以及col元素的位置

Then, in the template matching row you can use this parameter, along with the position of the col element

<data col="{position()}" row="{$row}">
    <xsl:value-of select="."/>
</data>

注意这里使用花括号来创建属性.这些被称为属性值模板

Note the use of curly braces here to create the attributes. These are known as Attribute Value Templates

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>
    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="row"/>
        </root>
    </xsl:template>

    <xsl:template match="col">
        <xsl:param name="row" />
        <data col="{position()}" row="{$row}">
            <xsl:value-of select="."/>
        </data>
    </xsl:template>

    <xsl:template match="row">
    <xsl:apply-templates select="col">
        <xsl:with-param name="row" select="position()" />
    </xsl:apply-templates>
    </xsl:template>
</xsl:stylesheet>

这篇关于动态增加属性值 XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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