加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xslt – 列出XML文件中的每个节点

发布时间:2020-12-16 07:58:57 所属栏目:百科 来源:网络整理
导读:简单的情况……对于任何随机的XML文件,我想创建一个包含它的每个节点的列表,但没有任何重复!所以类似于: root name="example" child id="1" grandchild/ /child child id="2"/ child id="3"//root 被翻译成: /root/root/@name/root/child/root/child/@id/
简单的情况……对于任何随机的XML文件,我想创建一个包含它的每个节点的列表,但没有任何重复!所以类似于:
<root name="example">
  <child id="1">
    <grandchild/>
  </child>
  <child id="2"/>
  <child id="3"/>
</root>

被翻译成:

/root
/root/@name
/root/child
/root/child/@id
/root/child/grandchild

如何通过使用XSLT来做到这一点?

只是为了好玩,没有扩展功能.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="text()"/>
    <xsl:template match="*|@*">
        <xsl:param name="pPath"/>
        <xsl:param name="pNames" select="'&#xA;'"/>
        <xsl:variable name="vPath"
                      select="concat($pPath,'/',substring('@',1 div (count(.|../@*) =
                                                      count(../@*))),name())"/>
        <xsl:variable name="vNames">
            <xsl:if test="not(contains($pNames,concat('&#xA;',$vPath,'&#xA;')))">
                <xsl:value-of select="concat($vPath,'&#xA;')"/>
            </xsl:if>
            <xsl:apply-templates select="*[1]|@*">
                <xsl:with-param name="pPath" select="$vPath"/>
                <xsl:with-param name="pNames" select="$pNames"/>
            </xsl:apply-templates>
        </xsl:variable>
        <xsl:value-of select="$vNames"/>
        <xsl:apply-templates select="following-sibling::*[1]">
            <xsl:with-param name="pPath" select="$pPath"/>
            <xsl:with-param name="pNames" select="concat($pNames,$vNames)"/>
        </xsl:apply-templates>
    </xsl:template>
</xsl:stylesheet>

输出:

/root
/root/@name
/root/child
/root/child/@id
/root/child/grandchild

编辑:XSLT / XPath 2.0的更好示例.这个XPath 2.0行:

string-join(
   distinct-values(
      (//*|//@*)
         /string-join(
            (ancestor::node()/name(),if (self::attribute())
                then concat('@',name())
                else name()),'/')),'&#xA;')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读