我可以使用xpath设置属性名称的值吗?

时间:2010-12-27 16:23:34

标签: xml xslt xpath

我有一个像这样的标准元标记:

<meta name="desc" content="test"/>

我想转换成这样的新节点:

<xsl:template match="meta">
 <content>
  <xsl:attribute name="test"/>
 </content>
</xsl:template>

有没有办法用'和'xpath表达式替换'test'作为属性名?我可以以某种方式使用元标记中的@name吗?

1 个答案:

答案 0 :(得分:3)

  

有没有办法将'test'替换为   属性名称和xpath   表达

是的,使用属性值模板,如:

编辑:更清楚一点,这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="meta">
        <content>
            <xsl:attribute name="{@name}">
                <xsl:value-of select="@content"/>
            </xsl:attribute>
        </content>
    </xsl:template>
</xsl:stylesheet>

输出:

<content desc="test" />

编辑:根据Dimitre的猜测,如果你想要

<content name="desc">test</content>

使用此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="meta">
        <content name="{@name}">
            <xsl:value-of select="@content"/>
        </content>
    </xsl:template>
</xsl:stylesheet>