【发布时间】:2020-01-20 23:06:44
【问题描述】:
我正在做一个 xsl 转换,它将 ISO 19139-2 (2009) XML 文档转换为一些文本字符串(一个 GeoNetwork 模式插件的 index-fields/index.xsl,它反过来由 ElasticSearch 实例读取)。在特定元素中,我试图:
- 选择具有特定确切节点名称的该节点的所有后代
- 获取上述节点的所有立即子节点,除了具有特定名称的节点
- 打印上述节点的名称和值
但是,我似乎从未从转换中获得任何输出。从其他例子看,我觉得我一定遗漏了一些非常简单的东西,但我不能完全确定它。
我尝试了几种不同的方法,主要围绕如何放置无命名空间节点、我对某些路径的具体程度以及句点/当前节点位置元素的使用。
据我所知,我选择了所有可用的 MI_AcquisitionInformation,然后从那里寻找具有指定名称的孩子,然后抓住他们的孩子,加上特定的名称条件,然后读出这些孩子的值节点及其名称。但相反,我什么也没得到。
<xsl-for-each select="gmi:acquisitionInformation/*">
<xsl:for-each select=".//gmi:MI_Operation/*[name() != 'gmi:platform']">
<operationDetails> <!-- lines like this, without a namespace, end up being the name of the 'bundle' holding the resulting output' -->
<xsl:value-of select="name()"/>:<xsl:value-of select="."/>
</operationDetails>
</xsl:for-each>
<xsl:for-each select=".//gmi:MI_Platform/*[name() != 'gmi:instrument']">
<platformDetails>
<xsl:value-of select="name()"/>:<xsl:value-of select="."/>
</platformDetails>
</xsl:for-each>
<xsl:for-each select=".//gmi:MI_Instrument">
<instrumentDetails>
<xsl:value-of select="name()"/>:<xsl:value-of select="."/>
</instrumentDetails>
</xsl:for-each>
</xsl-for-each>
一个具体的详细示例是......
<gmi:acquisitionInformation>
<gmi:MI_AcquisitionInformation>
<gmi:instrument>
<gmi:MI_Instrument>
<gmi:identifier>
(additional content in nodes)
</gmi:identifier>
<gmi:type>
<gco:CharacterString> Text </gco:CharacterString>
</gmi:type>
</gmi:MI_Instrument>
</gmi:instrument>
<gmi:operation>
<gmi:MI_Operation>
<gmi:identifier>
(additional content in nodes)
</gmi:identifier>
<gmi:citation>
(additional content in nodes)
</gmi:citation>
<gmi:status>
<gmd:MD_ProgressCode (content in attributes)/>
</gmi:status>
<gmi:platform>
<gmi:MI_Platform>
<gmi:identifier>
(additional content in nodes)
</gmi:identifier>
<gmi:description>
<gco:CharacterString> Text </gco:CharacterString>
</gmi:description>
<gmi:instrument>
<gmi:MI_Instrument>
<gmi:identifier>
(additional content in nodes)
</gmi:identifier>
<gmi:type>
<gco:CharacterString> Text </gco:CharacterString>
</gmi:type>
</gmi:MI_Instrument>
</gmi:instrument>
</gmi:MI_Platform>
</gmi:platform>
</gmi:MI_Operation>
</gmi:operation>
<gmi:platform>
<gmi:MI_Platform>
<gmi:identifier>
(additional content in nodes)
</gmi:identifier>
<gmi:description>
<gco:CharacterString> Text </gco:CharacterString>
</gmi:description>
<gmi:instrument>
<gmi:MI_Instrument>
<gmi:identifier>
(additional content in nodes)
</gmi:identifier>
<gmi:type>
<gco:CharacterString> Text </gco:CharacterString>
</gmi:type>
</gmi:MI_Instrument>
</gmi:instrument>
</gmi:MI_Platform>
</gmi:platform>
</gmi:MI_AcquisitionInformation>
</gmi:acquisitionInformation>
我希望上述示例会出现这样的情况。
<operationDetails>
gmi:identifier:(the additional content from its child nodes)
gmi:citation:(the additional content from its child nodes)
gmi:status:(Unsure? maybe nothing because the values are in attributes here?)
<!-- note here that there is no platform information -->
</operationDetails>
<platformDetails>
gmi:identifier:(for the one inside the operation)
gmi:description:(for the one inside the operation)
</platformDetails>
<platformDetails>
gmi:identifier:(for the one inside MI_AcquisitionInformation)
gmi:description:(for the one inside MI_AcquisitionInformation)
<!-- note here that there is no instrument information -->
</platformDetails>
<instrumentDetails>
gmi:identifier:(for the one inside MI_AcquisitionInformation)
gmi:type:(for the one inside MI_AcquisitionInformation)
</instrumentDetails>
<instrumentDetails>
for the one inside platform inside the operation, same sort of info as above...
</instrumentDetails>
<instrumentDetails>
for the one inside the platform inside MI_AcquisitionInformation, same sort of info as above...
</instrumentDetails>
大多数情况下,对于包含 MI_Operation、MI_Platform 和/或 MI_Instrument 的记录,我希望在 ElasticSearch 中看到名为 (something)Details 的内容,但这是在另一个处理步骤之后。
但如前所述,我什么也没得到,也没有错误消息 - 所以大概我只是在错误的节点上选择,而不是在代码中实际出错。
【问题讨论】:
-
考虑发布最少但完整的示例,在没有命名空间声明的情况下向我们展示 XML 或 XSLT 中的命名空间前缀是没有意义的。
-
我同意@MartinHonnen,但请注意
xsl-for-each应该是xsl:for-each。 -
@kjhughes ack,谢谢你的收获。我还一夜之间意识到我的代码并不理想。我会尝试一些事情,然后编辑问题。回复:最小但完整,抱歉,我认为这对于这个问题来说已经足够完整了。我会稍微扩展一些示例。
标签: xml xpath xslt-2.0 geonetwork