Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 XPath是否只能返回具有名称空间的子节点为X的节点?_Xml_Xpath - Fatal编程技术网

Xml XPath是否只能返回具有名称空间的子节点为X的节点?

Xml XPath是否只能返回具有名称空间的子节点为X的节点?,xml,xpath,Xml,Xpath,这个问题是问题的继续 我想找到所有有foo的“宠物”,其中一些宠物有名称空间,而其他宠物没有 是否可以使用XPath仅选择具有特定子元素的节点?例如,从这个XML中,我只希望pets中的元素具有“bar”子元素。因此,生成的数据集将包含lizard和pig元素 <pets xmlns="urn:cat-org:v1"> <cat> <foo>don't care about this</foo> </cat> <

这个问题是问题的继续

我想找到所有有foo的“宠物”,其中一些宠物有名称空间,而其他宠物没有

是否可以使用XPath仅选择具有特定子元素的节点?例如,从这个XML中,我只希望pets中的元素具有“bar”子元素。因此,生成的数据集将包含lizard和pig元素

<pets xmlns="urn:cat-org:v1">
  <cat>
    <foo>don't care about this</foo>
  </cat>
  <dog xmlns:v1="urn:cat-org:v1">
     <v1:foo>not this one either</foo>
  </dog>
  <lizard>
     <bar>lizard should be returned, because it has a child of bar</bar>
  </lizard>
  <pig xmlns:v55="urn:cat-org:v1">
     <v55:bar>return pig, too</bar>
  </pig>
</pets>

别在乎这个
也不是这个
蜥蜴应该被归还,因为它有一个巴尔的孩子
猪也回来了
任何建议都会让人感动

例如,从这个XML中,我只希望pets中具有 “酒吧”的孩子

使用

/*/*[*[local-name() = 'bar']]
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select="/*/*[*[local-name() = 'bar']]"/>
 </xsl:template>
</xsl:stylesheet>
<lizard xmlns="urn:cat-org:v1">
   <bar>lizard should be returned, because it has a child of bar</bar>
</lizard>
<pig xmlns="urn:cat-org:v1" xmlns:v55="urn:cat-org:v1">
   <v55:bar>return pig, too</v55:bar>
</pig>
这将选择顶部元素的所有子元素,这些子元素至少有一个子元素具有本地名称
“bar”
(无论是否存在前缀)

基于XSLT的验证

/*/*[*[local-name() = 'bar']]
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select="/*/*[*[local-name() = 'bar']]"/>
 </xsl:template>
</xsl:stylesheet>
<lizard xmlns="urn:cat-org:v1">
   <bar>lizard should be returned, because it has a child of bar</bar>
</lizard>
<pig xmlns="urn:cat-org:v1" xmlns:v55="urn:cat-org:v1">
   <v55:bar>return pig, too</v55:bar>
</pig>

在提供的XML文档上应用此转换时(更正为格式正确):


别在乎这个
也不是这个
蜥蜴应该被归还,因为它有一个巴尔的孩子
猪也回来了
计算XPath表达式并将所选元素复制到输出中

/*/*[*[local-name() = 'bar']]
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select="/*/*[*[local-name() = 'bar']]"/>
 </xsl:template>
</xsl:stylesheet>
<lizard xmlns="urn:cat-org:v1">
   <bar>lizard should be returned, because it has a child of bar</bar>
</lizard>
<pig xmlns="urn:cat-org:v1" xmlns:v55="urn:cat-org:v1">
   <v55:bar>return pig, too</v55:bar>
</pig>

蜥蜴应该被归还,因为它有一个巴尔的孩子
猪也回来了

谢谢你,伙计。我在[[使用local-name()]之间缺少*号