Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 根据子节点的内容选择子节点_Xml_Xslt_Xpath_Nodes - Fatal编程技术网

Xml 根据子节点的内容选择子节点

Xml 根据子节点的内容选择子节点,xml,xslt,xpath,nodes,Xml,Xslt,Xpath,Nodes,XML代码段: <AA> <BB>foo</BB> <CC>bar</CC> <DD>baz</DD> <EE>bar</EE> </AA> OP问题的最简单解决方案之一是以下XPath表达式: 请注意,没有涉及XSLT指令-这只是一个XPath表达式,因此问题只能标记为XPath 从这里开始,可以以各种方式在XSLT中使用此XPath表达式,例如在所有选定节

XML代码段:

<AA>
  <BB>foo</BB>
  <CC>bar</CC>
  <DD>baz</DD>
  <EE>bar</EE>
</AA>
OP问题的最简单解决方案之一是以下XPath表达式:

请注意,没有涉及XSLT指令-这只是一个XPath表达式,因此问题只能标记为XPath

从这里开始,可以以各种方式在XSLT中使用此XPath表达式,例如在所有选定节点上应用模板

例如,下面是一个XSLT转换,它接受XML文档并生成另一个XML文档,其中删除了所有元素(其内容不等于bar的子元素):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="AA">
      <xsl:copy>
         <xsl:apply-templates select="*[. = 'bar']"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

谢谢你的另一篇文章也很有帮助,因为下一个扩展将使用正则表达式进行选择:@zhenyou是受欢迎的。请记住,正则表达式解决方案是在XPath2.0中实现的——在XPath1.0中没有这样方便的方法
*/*[.='bar']
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="AA">
      <xsl:copy>
         <xsl:apply-templates select="*[. = 'bar']"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
<AA>
    <BB>foo</BB>
    <CC>bar</CC>
    <DD>baz</DD>
    <EE>bar</EE>
</AA>
<AA>
   <CC>bar</CC>
   <EE>bar</EE>
</AA>
*[. = 'bar']