2012-01-05 108 views
0

多次我有这样一个节点:Xpath的选择节点依赖属性的XSL转换

<foo my:first="yes" my:second="no">text</foo> 

我需要它选择在“我的”命名空间属性的每个节点XPath查询或XSLT的功能。但是如果一个元素具有多个“我的”属性,则该元素需要被多次选择。

目前,我有这样的:

<xsl:template match="//*[@my:*]"> 
    <bar> 
    <xsl:variable name="attributeName" select="local-name(./@my:*)" /> 
    <xsl:variable name="attributeValue" select="./@my:*" /> 
    <xsl:attribute name="name"> 
     <xsl:value-of select="$attributeName" /> 
    </xsl:attribute> 
    <xsl:attribute name="value"> 
     <xsl:value-of select="$attributeValue" /> 
    </xsl:attribute> 
    <xsl:value-of select="." /> 
    <bar> 
</xsl:template> 

,自然,它仅支持单一的“我”的属性,这会导致这样的转变:

<bar name="attr_in_my_namespace" value="value_of_that_attr">text</bar> 

如果我尝试这与节点我在开头介绍,我得到以下错误:

A sequence of more than one item is not allowed as the first argument of 
    local-name() (@my:first, @my:second) 

因此,预期的结果wo可以是:

<bar name="first" value="yes">text</bar> 
<bar name="second" value="no">text</bar> 

我该如何做到这一点?

回答

2

在我看来,如果你只是想处理属性节点例如

<xsl:template match="*/@my:*"> 
    <bar name="{local-name()}" value="{.}"> 
    <xsl:value-of select=".."/> 
    </bar> 
</xsl:template> 

然后

<xsl:template match="*[@my:*]"> 
    <xsl:apply-templates select="@my:*"/> 
</xsl:template> 
+0

你可能是恰到好处!感谢您的帮助,我会尝试一下。 – Alp 2012-01-05 10:39:33

+0

如何在属性模板中选择节点的内部文本?我需要将元素的内容放在'bar'标签之间。 – Alp 2012-01-05 10:45:33

+0

我编辑了第一个模板,就像在我的第一个答案尝试中一样,我忽略了你也想输出元素内容。 – 2012-01-05 10:46:12