【问题标题】:XPath concat all matches of sibling nodesXPath 连接所有兄弟节点的匹配项
【发布时间】:2015-11-04 16:08:32
【问题描述】:

我没有在类似的帖子中找到解决方案,所以我希望有人可以帮助我。我的 XML 如下(摘录):

<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 执行此操作。

我发现评估返回一个字符串,而不是一个节点集,我可能需要有多个结果。哪个 XPath 会返回一个 NODESET?

感谢您的帮助!

【问题讨论】:

    标签: xpath saxon xpath-2.0


    【解决方案1】:

    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。

    【讨论】:

      【解决方案2】:

      我怀疑字符串和节点集的混淆是因为您使用的是 JAXP API,它是为 XPath 1.0 设计的,不允许您充分利用 XPath 2.0 的灵活性。如果您想从 XPath 表达式返回一系列字符串,正如@Martin Honnen 建议的那样,那么您将需要使用 s9api API:它处理完整的 XPath 2.0 数据模型。您无法使用 JAXP 和节点集结果绕过此限制,因为 XPath 不允许您创建新节点(仅用于选择现有节点),并且您想要的字符串与现有节点不对应。

      但是,如果您真的受限于 JAXP,那么您可以使用 string-join() 函数更改查询以将结果组合成单个字符串,并使用一些合适的分隔符(例如换行符)和拆分通过在调用 Java 代码中进行标记,它可以返回多个结果。

      【讨论】:

        【解决方案3】:

        只需使用

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

        哪个 XPath 会返回一个 NODESET?

        Xpath 3.0 表达式可以使用标准函数生成节点(-set),例如 parse-xml()parse-xml-fragment()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多