我有以下代码块来获取树中节点的名称,如下所示:
section/page/subPage
但我希望能够将其归结为以下内容(只是补上):
section[@id='someId']/page/subPage[@user='UserA']/@title
我从其中一个StackOverflow帖子中找到了以下代码:
<xsl:attribute name="path">
<xsl:for-each select="ancestor-or-self::*">
<xsl:if test="name() != 'root'">
<xsl:value-of select="name()">
<xsl:if test="not(position()=last())">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:value-of>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
这给了我直接的路径,但是我想在它上运行更多的逻辑来使它包含@id(或相关属性),以及可能还有一些我目前无法想到的东西。
这样做的最佳方式是什么?
我已经检查了EXSLT功能,这可能有用,但也许你们已经用更好的方法解决了这个问题。
有什么想法吗?
如果有帮助的话,我正在使用ruby的nokogiri来解析xml / xslt。
非常感谢, 兰斯
答案 0 :(得分:4)
此解决方案可以满足您的需求:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:template match="/">
<root>
<xsl:attribute name="path">
<xsl:apply-templates select="(//@title)[1]" mode="make-path" />
</xsl:attribute>
</root>
</xsl:template>
<xsl:template match="*|@*" mode="make-path">
<xsl:apply-templates select="parent::*" mode="make-path" />
<xsl:text>/</xsl:text>
<xsl:apply-templates select="." mode="make-name" />
<xsl:choose>
<xsl:when test="self::section">
<xsl:apply-templates select="@id" mode="make-predicate" />
</xsl:when>
<xsl:when test="self::subPage">
<xsl:apply-templates select="@user" mode="make-predicate" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="*|@*" mode="make-predicate">
<xsl:text>[</xsl:text>
<xsl:apply-templates select="." mode="make-name" />
<xsl:text> = '</xsl:text>
<xsl:value-of select="." />
<xsl:text>']</xsl:text>
</xsl:template>
<xsl:template match="*" mode="make-name">
<xsl:value-of select="name()" />
</xsl:template>
<xsl:template match="@*" mode="make-name">
<xsl:text>@</xsl:text>
<xsl:value-of select="name()" />
</xsl:template>
</xsl:stylesheet>
适用于
<section id="someId">
<page>
<subPage user="UserA" title="test" />
<subPage user="UserB" title="blah" />
</page>
<page>
<subPage user="UserC" title="fooh" />
</page>
</section>
你得到:
<root path="/section[@id = 'someId']/page/subPage[@user = 'UserA']/@title" />
<xsl:choose>
是可配置的位置(根据需要添加<xsl:when>
个):
<!-- test for element name -->
<xsl:when test="self::section">
<!-- make predicates out of selected attributes -->
<xsl:apply-templates select="@id" mode="make-predicate" />
</xsl:when>
也可能:
<xsl:when test="self::section">
<xsl:apply-templates select="@name|@category|subElement" mode="make-predicate" />
</xsl:when>
会导致
<root path="/section[@name = 'someName'][@category = 'somecat'][subElement = 'xyz']/..." />
我看到的唯一问题是谓词值包含单引号。他们会打破XPath。
答案 1 :(得分:1)
对于每个祖先节点,您可以使用简单的xsl:for-each
遍历所有属性<xsl:for-each select="@*">
然后,您可以使用这些属性来构建XPath字符串
<xsl:for-each select="ancestor-or-self::*">
<xsl:if test="name() != 'root'">
<xsl:value-of select="name()"/>
<xsl:if test="@*">
<xsl:text>[</xsl:text>
<xsl:for-each select="@*">
<xsl:text>@</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>='</xsl:text>
<xsl:value-of select="."/>
<xsl:text>'</xsl:text>
<xsl:if test="not(position()=last())">
<xsl:text> and </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:if>
<xsl:if test="not(position()=last())">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>