2013-10-08 69 views
0

這是基於相同的輸入數據,在這個問題:Use XSLT/XPATH to select elements having a child element with a specific value選擇XML節點基於兩種不同的屬性

不過,我需要現在只選擇<file>元素,其中:

  1. 至少一個<shared_element>啓動 「$ /貝塔」
  2. <user>是 「約翰」

.2。是前面問題的唯一補充...我嘗試添加額外的測試,但是我的XSLT太糟糕了,無法理解如何執行此操作。理想情況下,我想知道如何在接受的答案中修改XSL,但一般的「如何在單獨的元素/屬性上要求值」示例就好了。

回答

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>