java - 使用 XPath 选择 XML 节点时如何忽略 namespace

标签 java xpath

我看到有一个选项可以忽略 namespace

 xpathUtil.getObjectValue("//*[local-name() = 'object name']")

是否可以添加更多对象名称

例如

  <Schema xmlns:m...... Namespace="app"
   xmlns:d = ....
  <complexType Name = "Comp>
  <EntityType Name = "a">
  <EntityType Name = "b">
  </Schema>

 <Schema xmlns:m...... Namespace="app2"
   xmlns:d = ....
  <complexType Name = "Comp2>
  <EntityType Name = "a2">
  <EntityType Name = "b2">
  </Schema>

是否可以仅针对 Schema= app 获取属性 EntityType 的数据? (意思是得到a和b的结果,而不是a2和b2的结果)

最佳答案

假设输入文件的完整版本可能如下所示

<?xml version="1.0" encoding="ISO-8859-1"?>
<Schemas>
  <ns1:Schema xmlns:ns1="app1" >
    <ns1:complexType Name = "Comp1">
      <ns1:EntityType Name = "a1"/>
      <ns1:EntityType Name = "b1"/>
    </ns1:complexType>
  </ns1:Schema>

  <ns2:Schema xmlns:ns2="app2">
    <ns2:complexType Name = "Comp2">
      <ns2:EntityType Name = "a2"/>
      <ns2:EntityType Name = "b2"/>
    </ns2:complexType>
  </ns2:Schema>
</Schemas>

下面的 XSLT 显示了如何通过命名空间的 URI 和命名空间的名称来过滤标签。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:variable name="ns_uri1" select="'app1'"/>
  <xsl:variable name="ns_uri2" select="'app2'"/>
  <xsl:variable name="ns_name1" select="'ns1'"/>
  <xsl:variable name="ns_name2" select="'ns2'"/>

  <xsl:template match="/Schemas">

    <apps>
      <app1_selected_by_namespace_uri>
        <xsl:copy-of select="*[local-name(.) = 'Schema' and namespace-uri(.)=$ns_uri1]"/>
      </app1_selected_by_namespace_uri>

      <app2_selected_by_namespace_uri>
        <xsl:copy-of select="*[local-name(.) = 'Schema' and namespace-uri(.)=$ns_uri2]"/>
      </app2_selected_by_namespace_uri>

      <app1_selected_by_namespace_name>
        <xsl:copy-of select="*[name(.) = concat($ns_name1, ':Schema')]"/>
      </app1_selected_by_namespace_name>

      <app2_selected_by_namespace_name>
        <xsl:copy-of select="*[name(.) = concat($ns_name2, ':Schema')]"/>
      </app2_selected_by_namespace_name>
    </apps>
  </xsl:template>

</xsl:stylesheet>

产生以下输出文档

<?xml version="1.0" encoding="UTF-8"?>
<apps>
  <app1_selected_by_namespace_uri>
    <ns1:Schema xmlns:ns1="app1">
    <ns1:complexType Name="Comp1">
      <ns1:EntityType Name="a1"/>
      <ns1:EntityType Name="b1"/>
    </ns1:complexType>
  </ns1:Schema>
  </app1_selected_by_namespace_uri>
  <app2_selected_by_namespace_uri>
    <ns2:Schema xmlns:ns2="app2">
    <ns2:complexType Name="Comp2">
      <ns2:EntityType Name="a2"/>
      <ns2:EntityType Name="b2"/>
    </ns2:complexType>
  </ns2:Schema>
  </app2_selected_by_namespace_uri>
  <app1_selected_by_namespace_name>
    <ns1:Schema xmlns:ns1="app1">
    <ns1:complexType Name="Comp1">
      <ns1:EntityType Name="a1"/>
      <ns1:EntityType Name="b1"/>
    </ns1:complexType>
  </ns1:Schema>
  </app1_selected_by_namespace_name>
  <app2_selected_by_namespace_name>
    <ns2:Schema xmlns:ns2="app2">
    <ns2:complexType Name="Comp2">
      <ns2:EntityType Name="a2"/>
      <ns2:EntityType Name="b2"/>
    </ns2:complexType>
  </ns2:Schema>
  </app2_selected_by_namespace_name>
</apps>

在这两种情况下,名称都不是硬编码的,而是由参数给出的。我想其中一个版本会满足您的需求。您将在 copy-of 标记的 select 属性中找到 XPath 表达式。

关于java - 使用 XPath 选择 XML 节点时如何忽略 namespace ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19770553/

相关文章:

java - 指定任务可执行位置 "C:\Program Files (x86)\Java\jdk1.7.0_71\\bin\javac.exe"无效

java - 从 ArrayList<Object> 转换为 ArrayList<Custom> 类会引发错误,而从 Object 转换为 Custom 则不会

JavaScript 返回克隆不是 countMembers 的函数

java - 在回调外部更新变量

java - 当未使用或设置节点内定义的 Freemarker 变量时删除 XML 节点

php - 使用命名空间解析 XML 响应

java - 如何将文件名分为基本名和扩展名

java - XJC [错误] "//*[local-name()=' 架构的 XPath 评估']“导致目标节点为空

python - 抓取削时如何消除某些元素?

.net - 我可以在 XPath 表达式中使用 Regex 吗?