这个 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')]]]"/>
</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 时(与您一样,但添加了额外的不匹配文件):
<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 '1.2.3.4'</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 '1.2.3.4'</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:
<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>