0

这基于与此问题相同的输入数据:使用 XSLT/XPATH 选择具有特定值的子元素的元素

但是,我现在只需要选择<file>以下元素:

  1. 至少有一个<shared_element>以“$/Beta”开头
  2. <user>是“约翰”

.2. 是对上一个问题的唯一补充......我尝试添加一个额外的测试,但我的 XSLT 太糟糕了,无法理解如何做到这一点。理想情况下,我想知道如何在接受的答案中修改 XSL,但是一般的“如何要求单独元素/属性上的值”示例就可以了。

4

1 回答 1

1

您只需要在匹配的模板内修改 select 语句中的 XPath 谓词root。这是 XSLT 的修改版本:

<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')]] and user='John']"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="file">
    <xsl:copy>
      <xsl:apply-templates select="name | vss_path | shared_links | user"/>
    </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 时(添加了额外的测试用例):

<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>
  <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>Ben</user>
    <shared_links>
      <shared_link>$/Beta_1</shared_link>
      <shared_link>$/Branches/New_Feature</shared_link>
    </shared_links>
  </file>
</root>

它产生以下输出:

<root>
   <file>
      <name>file.bat</name>
      <vss_path>$/Code/file.bat</vss_path>
      <user>John</user>
      <shared_links>
         <shared_link>$/Beta_1</shared_link>
      </shared_links>
   </file>
</root>
于 2013-10-08T17:09:23.100 回答