xml 根据 XSLT 中的子节点值选择节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12557975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:41:49  来源:igfitidea点击:

Select node based on child node value in XSLT

xmlxsltxpathxslt-1.0

提问by Rahul

I would like to select only those node where child node value matches a certain value.

我只想选择子节点值与某个值匹配的那些节点。

Here is my orig XML:

这是我的原始 XML:

This is my orig XML

这是我的原始 XML

<Entry>
 <Name>AAA</Name>
 <line id="1">A</line>
 <line id="2">B</line>
</Entry>
<Entry>
 <Name>BBB</Name>
 <line id="1">C</line>
 <line id="2">D</line>
</Entry>
<Entry>
 <Name>AAA</Name>
 <line id="1">E</line>
 <line id="2">F</line>
</Entry>
<Entry>
 <Name>CCC</Name>
 <line id="1">G</line>
 <line id="2">H</line>
</Entry>

I would like to extract all entries where Name = 'AAA', so the result would be:

我想提取 Name = 'AAA' 的所有条目,所以结果是:

<Entry>
 <Name>AAA</Name>
 <line id="1">A</line>
 <line id="2">B</line>
</Entry>
<Entry>
 <Name>AAA</Name>
 <line id="1">E</line>
 <line id="2">F</line>
</Entry>

I am limited to using XSLT 1.0.

我仅限于使用 XSLT 1.0。

Please provide any guidance. I am stuck on how to drop all sub-nodes for others that do not match.

请提供任何指导。我被困在如何删除不匹配的其他子节点的所有子节点上。

regards, Rahul

问候, 拉胡尔

回答by xshoppyx

The following will select all entry nodes with subnodes 'Name' that equal AAA.

下面将选择子节点“Name”等于 AAA 的所有入口节点。

//Entry[Name = "AAA"]

回答by jpj

Try something like this (List element added to get well-formed xml):

尝试这样的事情(添加列表元素以获得格式良好的 xml):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <List>
      <xsl:apply-templates select="//Entry[Name='AAA']"/>
    </List>
  </xsl:template>

  <xsl:template match="Entry">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

回答by Marc B

How about

怎么样

//Name[text()='AAA']/..

find all Namenodes whose text content is AAA, then move up one level to Name's parent node, which'd be Entry.

找到所有Name文本内容为 AAA 的节点,然后向上移动一级到 Name 的父节点,即Entry.