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/

相关文章:

xml - XSLT 将新元素添加到 XML 的根元素

xml - WSO2 中 Xpath 2.0 中 If/else 语句的使用

java - 如何识别 MultiValueMap 中的重复键

java - 为什么 Java 中不使用 const 关键字?

java - 十六进制转字符串中文,中文十六进制2字节,英文十六进制1字节?

java - java.library.path 中没有 swt-mozilla-gtk-4332

java - 在 Selenium 3 中使用 xpath 定位元素时出现错误

用于 XML 的 SQL Server

json - Xidel json xpath - 如何获取多个元素的值

java - 釉面列表 - 默认排序?