根据两个不同的属性选择XML节点

时间:2013-10-08 16:42:17

标签: xml xslt xslt-1.0

这是基于与此问题相同的输入数据:Use XSLT/XPATH to select elements having a child element with a specific value

但是我现在只需选择<file>元素,其中:

  1. 至少有一个<shared_element>开始“$ / Beta”
  2. <user>是“John”
  3. 0.2。是上一个问题的唯一补充...我尝试添加一个额外的测试,但我的XSLT太糟糕了解如何做到这一点。理想情况下,我想知道如何在接受的答案中修改XSL,但一般的“如何在单独的元素/属性上要求值”示例就可以了。

1 个答案:

答案 0 :(得分:1)

您只需要在匹配root的模板内的select语句中修改XPath谓词。这是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>
相关问题