gpt4 book ai didi

xml - 如何从 XML 获取所有叶元素的 xpath?

转载 作者:数据小太阳 更新时间:2023-10-29 01:41:20 27 4
gpt4 key购买 nike

我想知道是否可以创建一个 XSLT 样式表来提取给定 XML 文件中所有叶元素的 XPATH。例如。对于

<?xml version="1.0" encoding="UTF-8"?>
<root>
<item1>value1</item1>
<subitem>
<item2>value2</item2>
</subitem>
</root>

输出为

/root/item1
/root/subitem/item2

最佳答案

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" indent="no" />

<xsl:template match="*[not(*)]">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/', name())"/>

<xsl:if test="count(preceding-sibling::*[name() = name(current())]) != 0">
<xsl:value-of select="concat('[', count(preceding-sibling::*[name() = name(current())]) + 1, ']')"/>
</xsl:if>
</xsl:for-each>
<xsl:text>&#xA;</xsl:text>
<xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="*">
<xsl:apply-templates select="*"/>
</xsl:template>

</xsl:stylesheet>

输出:

/root/item1
/root/subitem/item2

关于xml - 如何从 XML 获取所有叶元素的 xpath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9064951/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com