2015-11-04 105 views
1

我还没有发现类似的线程解决方案,所以我希望有人能帮助我。我有XML如下(节选):的XPath CONCAT同级的所有节点匹配

<root> 
<identificationInfo> 
<MD_DataIdentification> 
<descriptiveKeywords> 
      <MD_Keywords> 
       <keyword> 
        <gco:CharacterString>Atmospheric conditions</gco:CharacterString> 
       </keyword> 
       <type> 
        <MD_KeywordTypeCode codeListValue="theme"/> 
       </type> 
      </MD_Keywords> 
     </descriptiveKeywords> 
     <descriptiveKeywords> 
      <MD_Keywords> 
       <keyword> 
        <gco:CharacterString>Agriculture</gco:CharacterString> 
       </keyword> 
       <keyword> 
        <gco:CharacterString>Biodiversity</gco:CharacterString> 
       </keyword> 
       <type> 
        <MD_KeywordTypeCode codeListValue="socialBenefitArea"/> 
       </type> 
      </MD_Keywords> 
     </descriptiveKeywords> 

我想是连接类型和关键字的字符串,所以我获取列表看起来像以下:

theme:Atmospheric conditions 
socialBenefitArea:Agriculture 
socialBenefitArea:Biodiversity 

我尝试了以下解决方案(XPath 1.0或XPath 2.0都可以使用),但始终只有第一个匹配的“主题:大气条件”返回。

  • for $n in /*/gmd:identificationInfo/*/gmd:descriptiveKeywords/gmd:MD_Keywords return string-join(($n/gmd:type/*/@codeListValue, ':', $n/gmd:keyword/*/text()), '')
  • /*/gmd:identificationInfo/*/gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/concat(*/text(), ':', ../gmd:type/*/@codeListValue)
  • //gmd:descriptiveKeywords/*/string-join((gmd:type/*/@codeListValue, gmd:keyword/*/text()[1]), ':')
  • //gmd:descriptiveKeywords/*/gmd:keyword/concat(following-sibling::gmd:type/*/@codeListValue, ':', ./*/text())

如果XPath的看起来是正确的,我在Java中与Saxon-HE 9.x这样做。

我没查出是评估返回一个字符串,而不是一个NODESET,我可能需要有多个结果。哪个XPath会返回一个NODESET?

感谢您的帮助!

回答

0

中的XPath 2.0表达式//gco:CharacterString/concat(ancestor::MD_Keywords/type/MD_KeywordTypeCode/@codeListValue, ':', .)返回(http://xsltransform.net/6r5Gh2U)三串

theme:Atmospheric conditions 
socialBenefitArea:Agriculture 
socialBenefitArea:Biodiversity 

的顺序,我不明白为什么你问设置为XPath 2.0中的一个节点不返回节点集,但节点,而序列或原始值。当你的结果不会在节点包含但要连接包含在不同节点串,我怎么看不到输入选择的节点会有所帮助,如果你想,那么你需要XSLT或XQuery创建新的节点。

0

我怀疑有关字符串和节点集混淆的来源,因为你正在使用JAXP API,它是专为XPath 1.0和不允许利用XPath 2.0中的充分的灵活性。如果你想从你的XPath表达式返回一个字符串的序列,如@马丁Honnen建议,那么你将需要使用s9api API来代替:这个处理完整的XPath 2.0数据模型。您可以绕过这个限制使用JAXP和节点集的结果,因为XPath不允许你创建新的节点(只选择现有节点),你想不符合现有节点串不了。然而,如果你真的被约束到JAXP,那么你可以通过使用string-join()函数和一些合适的分隔符(例如换行符)来改变查询来将结果组合成单个字符串,并且通过在调用Java代码中进行标记,将它重新分成多个结果。

0

只需使用

/*/*/*/*/MD_Keywords/keyword/*/concat(../../type/*/@codeListValue, ': ', .) 

其中的XPath会返回一个NODESET?

Xpath 3。0表达式可以通过使用标准函数产生节点(-set),例如parse-xml()parse-xml-fragment()

相关问题