2012-01-05 104 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