【发布时间】:2012-06-29 07:51:59
【问题描述】:
编辑 - 我已经找到了解决问题的方法并发布了问答here。
我希望处理符合美国国会图书馆 EAD 标准(找到 here)的 XML。不幸的是,该标准在 XML 的结构方面非常松散。
例如,<bioghist> 标记可以存在于<archdesc> 标记中,也可以存在于<descgrp> 标记中,或者嵌套在另一个<bioghist> 标记中,或者是上述的组合,或者可以完全省略。我发现很难只选择我正在寻找的传记标签而不选择其他标签。
以下是我的 XSLT 可能需要处理的几个不同的 EAD XML 文档:
第一个例子
<ead>
<eadheader>
<archdesc>
<bioghist>one</bioghist>
<dsc>
<c01>
<descgrp>
<bioghist>two</bioghist>
</descgrp>
<c02>
<descgrp>
<bioghist>
<bioghist>three</bioghist>
</bioghist>
</descgrp>
</c02>
</c01>
</dsc>
</archdesc>
</eadheader>
</ead>
第二个例子
<ead>
<eadheader>
<archdesc>
<descgrp>
<bioghist>
<bioghist>one</bioghist>
</bioghist>
</descgrp>
<dsc>
<c01>
<c02>
<descgrp>
<bioghist>three</bioghist>
</descgrp>
</c02>
<bioghist>two</bioghist>
</c01>
</dsc>
</archdesc>
</eadheader>
</ead>
第三个例子
<ead>
<eadheader>
<archdesc>
<descgrp>
<bioghist>one</bioghist>
</descgrp>
<dsc>
<c01>
<c02>
<bioghist>three</bioghist>
</c02>
</c01>
</dsc>
</archdesc>
</eadheader>
</ead>
如您所见,EAD XML 文件可能几乎在任何地方都有<bioghist> 标签。我想产生的实际输出太复杂,无法在此处发布。上述三个 EAD 示例的输出简化示例可能如下:
第一个例子的输出
<records>
<primary_record>
<biography_history>first</biography_history>
</primary_record>
<child_record>
<biography_history>second</biography_history>
</child_record>
<granchild_record>
<biography_history>third</biography_history>
</granchild_record>
</records>
第二个例子的输出
<records>
<primary_record>
<biography_history>first</biography_history>
</primary_record>
<child_record>
<biography_history>second</biography_history>
</child_record>
<granchild_record>
<biography_history>third</biography_history>
</granchild_record>
</records>
第三个例子的输出
<records>
<primary_record>
<biography_history>first</biography_history>
</primary_record>
<child_record>
<biography_history></biography_history>
</child_record>
<granchild_record>
<biography_history>third</biography_history>
</granchild_record>
</records>
如果我想提取“第一个”传记值并将其放入<primary_record>,我不能简单地使用<xsl:apply-templates select="/ead/eadheader/archdesc/bioghist",因为该标签可能不是<archdesc> 标签的直接后代。它可能被<descgrp> 或<bioghist> 或它们的组合包裹。我不能select="//bioghist",因为这会拉出all <bioghist> 标签。我什至不能select="//bioghist[1]",因为那里实际上可能没有<bioghist> 标签,然后我将把值拉到<c01> 下面,这是“第二个”,应该稍后处理。
这已经是一篇很长的帖子,但另一个问题是<cxx> 节点的数量可以不受限制,最多嵌套十二层。我目前正在递归处理它们。我尝试将当前正在处理的节点(例如<c01>)保存为名为“RN”的变量,然后运行<xsl:apply-templates select=".//bioghist [name(..)=name($RN) or name(../..)=name($RN)]">。这适用于某些形式的 EAD,其中 <bioghist> 标记没有嵌套太深,但如果它必须处理由喜欢将标记包装在其他标记中的人创建的 EAD 文件,它将失败(根据符合 EAD 标准)。
我喜欢用某种方式表达
- 获取任何
<bioghist>标签在当前节点下的任意位置,但 - 如果您点击了
<c??>标签,请不要深入挖掘
我希望我已经说明了情况。如果我留下任何模棱两可的地方,请告诉我。您能提供的任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: xslt xpath xml-parsing