带有默认命名空间的Xpath

时间:2015-07-21 05:35:26

标签: java xml xslt xpath xslt-2.0

我试图获取某个XML元素的所有值。但是,未定义名称空间。我尝试过使用local-name()函数,但没有运气。

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
        <entry xml:base="https://www.website.com" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
          <id>A</id>
          <title type="text"></title>
          <updated>2015-07-21T02:40:30Z</updated>
          <author>
            <name />
          </author>
          <link rel="edit" title="Application" href="A(1347)" />
          <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/b" type="application/atom+xml;type=feed" title="B" href="A(1347)/B" />
          <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/C" type="application/atom+xml;type=feed" title="C" href="A(1347)/C" />
          <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/D" type="application/atom+xml;type=entry" title="D" href="A(1347)/D" />
          <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/E" type="application/atom+xml;type=feed" title="E" href="A(1347)/E">
            <m:inline>
              <feed>
                <title type="text">E</title>
                <id>1347</id>
                <updated>2015-07-21T02:40:30Z</updated>
                <link rel="self" title="E" href="A(1347)/E" />
                <entry>
                  <id>www.website.com/</id>
                  <title type="text"></title>
                  <updated>2015-07-21T02:40:30Z</updated>
                  <author>
                    <name />
                  </author>
                  <link rel="edit" title="E" href="E(4294)" />
                  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/A" type="application/atom+xml;type=entry" title="Application" href="E(4294)/A" />
                  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/D" type="application/atom+xml;type=entry" title="D" href="E(4294)/D" />
                  <category term="APIModel.FileBaseDocuments" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                  <content type="application/xml">
                    <m:properties>
                      <d:ID m:type="Edm.Int32">4294</d:ID>
                      <d:Type>123</d:Type>
                    </m:properties>
                  </content>
                </entry>
                 <entry>
              <id>www.website.com</id>
              <title type="text"></title>
              <updated>2015-07-21T02:40:30Z</updated>
              <author>
                <name />
              </author>
              <link rel="edit" title="E" href="E(4295)" />
              <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/A" type="application/atom+xml;type=entry" title="A" href="E(4295)/A" />
              <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/D" type="application/atom+xml;type=entry" title="D" href="E(4295)/D" />
              <category term="APIModel.FileBaseDocuments" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
              <content type="application/xml">
                <m:properties>
                  <d:ID m:type="Edm.Int32">4295</d:ID>
                  <d:Type>456</d:Type>
                </m:properties>
              </content>
             </entry>
           </feed>
          </m:inline>
        </link>
      </entry>

我想检索&#34; m:properties / d中的所有值:ID m:type =&#34; Edm.Int32&#34; (在这种情况下是4294)但我没有运气。所以对于一个文件,会有一个&#34; feed&#34;标签中填充了多个&#34;条目&#34;标签。在这些标签里面会有一个&#34; m:properties / d:ID m:type =&#34; Edm.Int32&#34;我需要检索。有关这种情况的正确xPath是什么的任何建议?

2 个答案:

答案 0 :(得分:1)

不使用local-name()求助于命名空间不可知的xml,为什么不为默认命名空间注册命名空间,例如x的{​​{1}}前缀,然后您的Xpath就像:

xmlns:x="http://www.w3.org/2005/Atom"

//x:feed/x:entry/x:content/m:properties/d:ID[@m:type='Edm.Int32'] 方法更详细:

local-name()

Example of both approaches here

答案 1 :(得分:0)

使用@exclude-result-prefixes属性过滤掉输出中无关的命名空间。你不需要在变换中声明http://www.w3.org/2005/Atom,除非你出于其他目的需要它,但即便如此,它也许是件好事。

只需匹配表达式m:properties/d:ID[@m:type='Edm.Int32']即可获得所需数据。

例如,此变换应用于给定的输入文档....

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
  xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
  xmlns:atom="http://www.w3.org/2005/Atom"
  version="2.0"
  exclude-result-prefixes="xsl d m atom">

<xsl:output encoding="utf-8" omit-xml-declaration="yes" indent="yes" />

<xsl:template match="/">
  <Edm.Int32s>
    <xsl:apply-templates />
  </Edm.Int32s>
</xsl:template>

<xsl:template match="*">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="text()" />

<xsl:template match="m:properties/d:ID[@m:type='Edm.Int32']">
  <value><xsl:value-of select="text()" /></value>
</xsl:template>  

</xsl:stylesheet>

......产出产量......

<edm.int32s>
  <value>4294</value>
  <value>4295</value>
</edm.int32s>

注意

我从你的问号标签中假设你想要一个XSLT转换。如果您只想将一个简单的XPath表达式直接应用于文档,则可以使用...

//m:properties/d:ID[@m:type='Edm.Int32']/text()

(当然,传递m和d的名称空间声明)。

相关问题