0

これは、この質問と同じ入力データに基づいています: XSLT/XPATH を使用して、特定の値を持つ子要素を持つ要素を選択します

<file>ただし、次の要素のみを選択する必要があります。

  1. 少なくとも 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 に答える