1

XML这是我在数据库中的一个示例:

<Device>
    <Name>device1</Name>
    <Sensors>
        <Sensor>
            <Name>sensor1</Name>
        </Sensor>
        <Sensor>
            <Name>sensor2</Name>
        </Sensor>
</Device>

我想为Field Range Index带有 Device 父元素的 Name 元素构建,但不是为 Sensor 中的 Name 元素构建。根据字段配置规则,我不能只将 Name 元素添加到字段中并排除 Sensors 元素。MarkLogic 5 中有解决方案吗?根据我的应用程序的要求,我无法转换文档和更改元素名称。

4

2 回答 2

2

简单的答案是“不”。但当然有前进的道路。

如果您愿意添加一个属性(但保持元素名称不变),您可以通过它来约束该字段,例如:

<Device>
  <Name _field="DeviceName">device1</Name>
  ...
</Device>

属性/值对可以是任何你想要的;你只需要告诉字段定义,它是什么。一个好的做法可能是namespace-qualify属性,所以它显然是来自不同词汇表的注释,例如:

<Device xmlns:field="http://example.com/field-annotations">
  <Name field:name="DeviceName">device1</Name>
  ...
</Device>

范围索引目前只能绑定到元素值、属性值和字段值。字段可以让您从底层结构中抽象一点,但不如更通用的机制那么抽象。

有时采用的另一种技术(尽管我没有亲自尝试过)是使用两个数据库,一个稍微更“合乎逻辑”(例如,标准格式,未更改),一个更database-optimized.

无论您决定采用哪种方法,都需要多做一些工作。

如果我是你,我可能会编写一个XSLT可以应用于输入的转换,以及另一个简单的转换,用于在需要时返回原始未注释版本。

注释.xsl:

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:field="http://example.com/field-annotations">

  <xsl:import href="copy.xsl"/>

  <!-- Annotate Device/Name as a field -->
  <xsl:template mode="add-att" match="Device/Name">
    <xsl:attribute name="field:name" select="'DeviceName'"/>
  </xsl:template>

</xsl:stylesheet>

清理.xsl:

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:field="http://example.com/field-annotations">

  <xsl:import href="copy.xsl"/>

  <!-- Delete all field:* attributes -->
  <xsl:template match="@field:*"/>

</xsl:stylesheet>

复制.xsl:

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Don't add any attributes by default -->
  <xsl:template mode="add-att" match="*"/>

  <!-- recursively copy elements -->
  <xsl:template match="*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates mode="add-att" select="."/>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <!-- copy attributes and child nodes -->
  <xsl:template match="@* | text() | comment() | processing-instruction()">
    <xsl:copy/>
  </xsl:template>

</xsl:stylesheet>

您可以在应用程序代码中应用annotate.xslusingxdmp:xslt-invoke()将文档插入数据库。或者,您可以annotate.xsl在 Information Studio 流中配置为转换器。

当您想检索原始文档时,只需调用:

xdmp:xslt-invoke("cleanup.xsl", doc("my-doc.xml"))

但是对于常规应用程序代码中的大多数情况,您不需要清理文档。因为,额外属性的存在很少会在那里产生任何影响。

于 2011-11-18T23:03:09.413 回答
1

从 Marklogic 6 开始,这正是路径范围索引旨在解决的问题:

http://developer.marklogic.com/blog/path-range-indexes

于 2013-05-20T23:49:34.813 回答