xml - 使用 XSLT 从任何 xml 中提取所有文本节点的 xpath 值
问题描述
我有一个xml:
<root>
<child attrib1="1">
<subChild>
<name>subChild1</name>
</subChild>
</child>
<child attrib1="2>
<subChild2>
<name>subChild2</name>
</subChild2>
</child>
我希望 xslt 生成 o/p 如下,即 xpath 及其值:
- /root/child[@attrib1="1]/subChild/name="subChild1"
- /root/child[@attrib1="2]/subChild2/name="subChild2"
解决方案
如评论中所述,您的问题并不完全清楚。尝试这样的事情作为你的起点:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:for-each select="//text()">
<xsl:apply-templates select="parent::*"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates select="parent::*"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:apply-templates select="@*"/>
</xsl:template>
<xsl:template match="@*">
<xsl:text>[@</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"]</xsl:text>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- python - 你如何检查某人是否加入了 Discord 中的语音频道,然后向某人发送 dm
- angular - Angular 11 - 在@component 声明中包含语言字典 - 如何最好地声明?
- dataframe - 如何根据条件替换列中的值
- flutter - 如何从 RaisedButton 创建 ElevatedButton?
- reactjs - Typescript Multiple API 以任意顺序更新状态
- android - 使用 Hilt 注入 WorkManager 但出现错误“Dagger 不支持提供 @AssistedInject 类型。”
- php - 你可以在 Laravel 中操作数据库列日期,然后使用 `whereDate()` 子句,而不使用 DB::raw?
- java - 每次单击按钮时如何创建一个新对象(矩形)?
- sql - 寻找替代 Postgres 的 CROSS JOIN LATERAL for Oracle
- mysql - 使用 docker-compose 多个 mysql 实例发送请求时出现错误 404