【问题标题】:Select XML nodes based on two different attributes根据两个不同的属性选择 XML 节点
【发布时间】:2013-10-17 13:45:02
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    您只需要修改模板内 select 语句中匹配 root 的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      相关资源
      最近更新 更多