XSLT转换:搜索节点,并返回分层父级 [英] XSLT Transformation: Search nodes, and return hierarchical parents

查看:260
本文介绍了XSLT转换:搜索节点,并返回分层父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这没有被问过,但是我有以下XML:

hoping this hasn't been asked before, but I have the following XML:

<Company id="1000" name="Company1000">
   <Company id="1020" name="Company1020" />
   <Company id="1004" name="Company1004">
      <Company id="1005" name="Company1005" />
   </Company>
   <Company id="1022" name="Company1022" />
</Company>

我具有以下XPath来搜索节点://*[contains(translate(@name, "ABCDEFGHJIKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "005")]

I have the following XPath to search for nodes: //*[contains(translate(@name, "ABCDEFGHJIKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "005")]

我想返回:

<Company id="1000" name="Company1000">
   <Company id="1004" name="Company1004">
      <Company id="1005" name="Company1005" />
   </Company>
</Company>

因此,这匹配Company1005节点及其所有父节点.如果我要搜索"100",我希望上面的内容也能返回,在这种情况下,它将依次匹配每个元素,但是我显然不希望重复节点.

So this matches the Company1005 node, and all its respective parents. I would like the above to also be returned if I were searching for "100", which in that case, would match each element in turn, but I clearly don't want duplication of nodes.

我已经为此奋斗了几个小时,所以您的帮助将不胜感激!!!

I've been struggling with this for hours now, so your help will be much appreciated!!!

谢谢.

推荐答案

解决方案是仅复制符合您的要求的后代节点或自身节点(包含所需的字符串).

The solution is to copy only descendant or self nodes which match your requirement (containing the string you want).

在此页面的底部找到该图片您需要的轴.

Look this picture at the bottom of this page for the XPath axes you need.

长版:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:strip-space elements="*"/>

<!-- Default: copy everything -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- just copy Company which descendant-or-self contain the matching string -->
<xsl:template match="Company">
    <xsl:if test="descendant-or-self::Company[contains(translate(@name, 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '005')]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

简短版本:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:strip-space elements="*"/>

<!-- Default: copy everything -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- do not copy Company which do not have a descendant-or-self matching string -->
<xsl:template match="Company[not(descendant-or-self::Company[contains(translate(@name, 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '005')])]"/>

</xsl:stylesheet>

您的xml的输出:

<?xml version="1.0" encoding="UTF-8"?>
<Company id="1000" name="Company1000">
  <Company id="1004" name="Company1004">
    <Company id="1005" name="Company1005"/>
  </Company>
</Company>

这篇关于XSLT转换:搜索节点,并返回分层父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆