xml - 获取 XSLT 当前节点,格式化为 XPath 查询?

标签 xml xslt xpath nokogiri

我有以下代码块获取树下节点的名称,如下所示:

节/页/子页

但我希望能够将其归结为以下内容(只是弥补):

部分[@id='someId']/page/subPage[@user='UserA']/@title

我从其中一篇 StackOverflow 帖子中找到了以下代码:


<code><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></code>

这给了我一条直路,但我想在它上面运行更多逻辑以使其包含@id(或相关属性),也许还有一些我现在想不到的东西。

执行此操作的最佳方法是什么?

我已经检查过 EXSLT 函数,它可能会起作用,但也许你们已经用更好的方法解决了这个问题。

有什么想法吗?

如果有帮助,我正在使用 ruby​​ 的 nokogiri 来解析 xml/xslt。

非常感谢, 兰斯

最佳答案

此解决方案可以满足您的需求:

<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> s,只要你喜欢):

<!-- 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。

关于xml - 获取 XSLT 当前节点,格式化为 XPath 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1496434/

相关文章:

java - XML 文件转 Json (org.json)

java - 如何将本地 DTD 文件的验证应用于 java 中的 xml 文件?

c# - 转换 XML 时路径异常中的非法字符

python - 该标签已存在于 HTML 代码中,但无法通过 xpath 到达

php - SimpleXml 如何正确设置编码和 xmls?

java - 相对布局参数无法正常工作

html - XSLT 合并具有相同值的行

xml - XSL 在 Google Chrome 中不起作用

Python ElementTree - 按顺序遍历子节点和文本

xpath - 如何使用 xpath 从占位符中获取值