使用 XSLT/XPATH 选择具有特定值的子元素的元素 [英] Use XSLT/XPATH to select elements having a child element with a specific value

查看:23
本文介绍了使用 XSLT/XPATH 选择具有特定值的子元素的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型(20 万行)XML 文件作为来自工具的报告(关于 VSS 数据库的报告).它由大量的 元素组成,如下所示:

I have a large (200k lines) XML file as a report from a tool (reporting on a VSS database). It consists of a large number of <file> elements like this:

<file>
 <name>file.bat</name>
 <version>111</version>
 <checkedout>No</checkedout>
 <binary>Text</binary>
 <vss_path>$/Code/file.bat</vss_path>
 <original_path>C:\code\file.bat</original_path>
 <action>Labeled &apos;1.2.3.4&apos;</action>
 <date>27/09/2013 09:08:00</date>
 <comment></comment>
 <label>1.2.3.4</label>
 <label_comment></label_comment>
 <user>John</user>
 <shared_links>
  <shared_link>$/Beta_1</shared_link>
  <shared_link>$/Branches/New_Feature</shared_link>
 </shared_links>
</file>

我想只找到 元素,其中至少有一个 以开头/前缀"$/Beta".

I want to find only the <file> elements which have at least one <shared_link> starting with/prefixed by "$/Beta".

在理想的世界中,我想要的每个匹配元素都是 和(匹配); 部分,但这并不重要.

In an ideal world, all I want for each matching element are the <name>, <vss_path> and (matching) <shared_link> parts, but that's not exactly important.

我不精通 XSLT/XPATH 但相信那些人可以做这样的事情吗?

I'm not well-versed in XSLT/XPATH but believe those can do something like this?

推荐答案

这个 XSLT 样式表:

This XSLT stylesheet:

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

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

  <xsl:template match="root">
    <xsl:copy>
      <xsl:apply-templates select="file[shared_links[shared_link[starts-with(., '$/Beta')]]]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="file">
    <xsl:copy>
      <xsl:apply-templates select="name | vss_path | shared_links"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="shared_links">
    <xsl:copy>
      <xsl:apply-templates select="shared_link[starts-with(., '$/Beta')]"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

应用于此输入 XML 时(与您的一样,但添加了一个额外的、不匹配的文件):

when applied to this input XML (as yours but with an extra, non-matching file added):

<root>
  <file>
    <name>file.bat</name>
    <version>111</version>
    <checkedout>No</checkedout>
    <binary>Text</binary>
    <vss_path>$/Code/file.bat</vss_path>
    <original_path>C:\code\file.bat</original_path>
    <action>Labeled &apos;1.2.3.4&apos;</action>
    <date>27/09/2013 09:08:00</date>
    <comment></comment>
    <label>1.2.3.4</label>
    <label_comment></label_comment>
    <user>John</user>
    <shared_links>
      <shared_link>$/Alpha_1</shared_link>
      <shared_link>$/Branches/New_Feature</shared_link>
    </shared_links>
  </file>
  <file>
    <name>file.bat</name>
    <version>111</version>
    <checkedout>No</checkedout>
    <binary>Text</binary>
    <vss_path>$/Code/file.bat</vss_path>
    <original_path>C:\code\file.bat</original_path>
    <action>Labeled &apos;1.2.3.4&apos;</action>
    <date>27/09/2013 09:08:00</date>
    <comment></comment>
    <label>1.2.3.4</label>
    <label_comment></label_comment>
    <user>John</user>
    <shared_links>
      <shared_link>$/Beta_1</shared_link>
      <shared_link>$/Branches/New_Feature</shared_link>
    </shared_links>
  </file>
</root>

产生以下输出 XML:

produces the following output XML:

<root>
  <file>
    <name>file.bat</name>
    <vss_path>$/Code/file.bat</vss_path>
    <shared_links>
      <shared_link>$/Beta_1</shared_link>
    </shared_links>
  </file>
</root>

这篇关于使用 XSLT/XPATH 选择具有特定值的子元素的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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