【问题标题】:Using XSLT extract xpath of all text nodes with value from any xml使用 XSLT 从任何 xml 中提取所有文本节点的 xpath 值
【发布时间】:2019-11-08 21:55:29
【问题描述】:

我有一个 xml:

<root>
 <child attrib1="1">
  <subChild>
   <name>subChild1</name>
  </subChild>
 </child>
 <child attrib1="2>
  <subChild2>
   <name>subChild2</name>
  </subChild2>
 </child>

我希望 xslt 生成 o/p 如下,即 xpath 及其值:

  1. /root/child[@attrib1="1]/subChild/name="subChild1"
  2. /root/child[@attrib1="2]/subChild2/name="subChild2"

【问题讨论】:

  • 您究竟要为哪些节点生成路径表达式?如果你有多个&lt;child attrib1="1"&gt;&lt;subChild&gt;&lt;name&gt;foo&lt;/name&gt;&lt;/subChild&gt;&lt;/child&gt; 元素会发生什么?您的路径似乎无助于唯一标识元素。另外,您使用哪个版本的 XSLT? XPath 3 有w3.org/TR/xpath-functions/#func-path,它肯定会为命名空间和重复的所有复杂情况生成更通用和更精确的路径。
  • 正如经常发生的那样,输入和相应输出的单个示例并不构成规范。
  • 抱歉,我在上面更正了预期的 o/p。一般来说,我想使用 xslt 为给定 xml 中的所有文本节点生成 xpath。

标签: xml xslt


【解决方案1】:

正如 cmets 中所述,您的问题并不完全清楚。尝试这样的事情作为你的起点:

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>"&#10;</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>

【讨论】:

  • 感谢 michael.hor257k 以上 xslt 工作并产生所需的结果!我对 xslt 非常陌生,尝试了另一种 xslt,如下面的 anwser 中提到的其他帖子所述。哪个是使用 xslt 学习/探索编程的最佳来源? w3schools 提供了非常基本的介绍,我在哪里可以学习到 xslt 中的高级编程/功能?
【解决方案2】:

参考我在 xslt 下面尝试的其他帖子,它会生成预期的输出:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="vApos">'</xsl:variable>

    <xsl:template match="*[not(*)]">
    <xsl:if test="not(*)">
     <xsl:apply-templates select="ancestor-or-self::*" mode="path"/>
     <xsl:value-of select="concat('=',$vApos,.,$vApos)"/>
     <xsl:text>&#xA;</xsl:text>
    </xsl:if>
    </xsl:template>

    <xsl:template match="*" mode="path">
    <xsl:value-of select="concat('/',name())"/>
    <xsl:apply-templates select="@*" mode="path"/>
    </xsl:template>

    <xsl:template match="@*" mode="path">
    <xsl:value-of select="concat('[@',name(), '=',$vApos,.,$vApos,']')"/>
    </xsl:template>
</xsl:stylesheet>

示例输入 xml:

<root id='1'>
    <elemA>one</elemA>
    <elemA attribute1='first' attribute2='second'>two</elemA>
    <elemB attribute='1'>three</elemB>
    <elemA >four</elemA>
    <elemC attribute='c'>
        <elemB attribute='2'>five</elemB>
        <elemB attribute='3'>five</elemB>
    </elemC>
</root>

输出:

/root[@id='1']/elemA='one'
/root[@id='1']/elemA[@attribute1='first'][@attribute2='second']='two'
/root[@id='1']/elemB[@attribute='1']='three'
/root[@id='1']/elemA='four'
/root[@id='1']/elemC[@attribute='c']/elemB[@attribute='2']='five'
/root[@id='1']/elemC[@attribute='c']/elemB[@attribute='3']='five'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多