xpath无法处理源xml中的名称空间

时间:2019-05-09 11:40:52

标签: xml xslt xpath

我有一个简单的xpath,这是由于源xml中存在名称空间而不起作用。并且编写xpath的工具没有任何功能可以用宿主语言(包括库)编写代码。我可以编写XSLT,但是我不确定该怎么做才能帮上忙吗?

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="https://collaboration.merck.com/sites/amseimcts/_api/">
   <id>d38a1da2-02e9-467e-b59b-e520c8607914</id>
   <title />
   <updated>2019-05-08T10:54:04Z</updated>
   <entry>
      <id>https://collaboration.xxx.com/sites/amseimcts/_api/Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared Documents/SAP Config/OBA1.csv')</id>
      <category term="SP.File" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
      <link rel="edit" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Author" type="application/atom+xml;type=entry" title="Author" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/Author" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CheckedOutByUser" type="application/atom+xml;type=entry" title="CheckedOutByUser" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/CheckedOutByUser" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EffectiveInformationRightsManagementSettings" type="application/atom+xml;type=entry" title="EffectiveInformationRightsManagementSettings" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/EffectiveInformationRightsManagementSettings" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/InformationRightsManagementSettings" type="application/atom+xml;type=entry" title="InformationRightsManagementSettings" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/InformationRightsManagementSettings" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ListItemAllFields" type="application/atom+xml;type=entry" title="ListItemAllFields" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/ListItemAllFields" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/LockedByUser" type="application/atom+xml;type=entry" title="LockedByUser" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/LockedByUser" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/ModifiedBy" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Properties" type="application/atom+xml;type=entry" title="Properties" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/Properties" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VersionEvents" type="application/atom+xml;type=feed" title="VersionEvents" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/VersionEvents" />
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Versions" type="application/atom+xml;type=feed" title="Versions" href="Web/GetFileByServerRelativePath(decodedurl='/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv')/Versions" />
      <title />
      <updated>2019-05-08T10:54:04Z</updated>
      <author>
         <name />
      </author>
      <content type="application/xml">
         <m:properties>
            <d:CheckInComment />
            <d:CheckOutType m:type="Edm.Int32">2</d:CheckOutType>
            <d:ContentTag>{87637609-7D7C-466B-A60B-5BD7A028712C},6,6</d:ContentTag>
            <d:CustomizedPageStatus m:type="Edm.Int32">0</d:CustomizedPageStatus>
            <d:ETag>"{87637609-7D7C-466B-A60B-5BD7A028712C},6"</d:ETag>
            <d:Exists m:type="Edm.Boolean">true</d:Exists>
            <d:IrmEnabled m:type="Edm.Boolean">false</d:IrmEnabled>
            <d:Length m:type="Edm.Int64">447</d:Length>
            <d:Level m:type="Edm.Byte">1</d:Level>
            <d:LinkingUri>https://collaboration.merck.com/sites/amseimcts/Shared%20Documents/SAP%20Config/OBA1.csv?d=w876376097d7c466ba60b5bd7a028712c</d:LinkingUri>
            <d:LinkingUrl>https://collaboration.merck.com/sites/amseimcts/Shared Documents/SAP Config/OBA1.csv?d=w876376097d7c466ba60b5bd7a028712c</d:LinkingUrl>
            <d:MajorVersion m:type="Edm.Int32">4</d:MajorVersion>
            <d:MinorVersion m:type="Edm.Int32">0</d:MinorVersion>
            <d:Name>OBA1.csv</d:Name>
            <d:ServerRelativeUrl>/sites/amseimcts/Shared Documents/SAP Config/OBA1.csv</d:ServerRelativeUrl>
            <d:TimeCreated m:type="Edm.DateTime">2019-05-05T10:50:02Z</d:TimeCreated>
            <d:TimeLastModified m:type="Edm.DateTime">2019-05-07T12:08:57Z</d:TimeLastModified>
            <d:Title m:null="true" />
            <d:UIVersion m:type="Edm.Int32">2048</d:UIVersion>
            <d:UIVersionLabel>4.0</d:UIVersionLabel>
            <d:UniqueId m:type="Edm.Guid">87637609-7d7c-466b-a60b-5bd7a028712c</d:UniqueId>
         </m:properties>
      </content>
   </entry>
</feed>

要求:我需要从给定的xml中提取 OBA1.csv 。我的下面的xpath无法正常工作。因此,我现在需要拿出XSLT来完成任务。

feed/entry/content/m:properties/d:Name/text()

1 个答案:

答案 0 :(得分:1)

如下更新您的xpath:

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

<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
    <xsl:value-of select="atom:feed/atom:entry/atom:content/m:properties/d:Name" />
</xsl:template>

</xsl:stylesheet>

在此处查看演示:https://xsltfiddle.liberty-development.net/94rmq6z