đánh giá năng động của một biểu thức XPath thường không được hỗ trợ trong XSLT (cả 1.0 và 2.0), tuy nhiên:
Chúng ta có thể thực hiện một đánh giá XPath động thay vì nói chung nếu chúng ta chỉ hạn chế mỗi đường dẫn vị trí là một yếu tố tên:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="inputId" select="'param/yyy/value'"/>
<xsl:variable name="vXpathExpression"
select="concat('root/meta/url_params/', $inputId)"/>
<xsl:template match="/">
<xsl:value-of select="$vXpathExpression"/>: <xsl:text/>
<xsl:call-template name="getNodeValue">
<xsl:with-param name="pExpression"
select="$vXpathExpression"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="getNodeValue">
<xsl:param name="pExpression"/>
<xsl:param name="pCurrentNode" select="."/>
<xsl:choose>
<xsl:when test="not(contains($pExpression, '/'))">
<xsl:value-of select="$pCurrentNode/*[name()=$pExpression]"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="getNodeValue">
<xsl:with-param name="pExpression"
select="substring-after($pExpression, '/')"/>
<xsl:with-param name="pCurrentNode" select=
"$pCurrentNode/*[name()=substring-before($pExpression, '/')]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
khi chuyển đổi này được áp dụng trên tài liệu XML này:
<root>
<meta>
<url_params>
<param>
<xxx>
<value>5</value>
</xxx>
</param>
<param>
<yyy>
<value>8</value>
</yyy>
</param>
</url_params>
</meta>
</root>
sự muốn, kết quả chính xác được sản xuất:
root/meta/url_params/param/yyy/value: 8
Nguồn
2011-01-13 16:32:10
cảm ơn. đây không phải là những gì tôi đã hỏi, nhưng nó chính xác những gì tôi cần) –
@igor milla: Bạn được chào đón. –