如何在 Xpath 1.0 中加入?

How to do joins in Xpath 1.0?

我想知道如何在 XPath 1.0 中进行连接。

我有一个 XML 文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <author>
       <id>MM</id>
       <gender>M</gender>
       <name>Max Mustermann</name>
   </author>
   <author>
       <id>JD</id>
       <gender>F</gender>
       <name>Joanne Doe</name>
   </author>
   <book>
      <author_id>MM</author_id>
      <title>Wie werde ich reich?</title>
      <isbn>123456</isbn>
   </book>
   <book>
      <author_id>JD</author_id>
      <title>Boring jokes</title>
      <isbn>4353442</isbn>
   </book>
</root>

我的目标是编写一个 XPath 1.0 查询来获取所有书名以及作者性别。我知道如何 select 书籍和作者性别,但如何将其结合起来?如何加入? 在SQL,我会写

SELECT book.title, author.gender FROM author, book WHERE book.author_id = author.id;

我不确定这是否足以满足您的需求,但是这个 xpath 表达式:

*//*[self::gender or self::title]

将select

M
F
Wie werde ich reich?
Boring jokes

编辑:表达式

(//*[self::gender])[1]

(//*[self::title])[1]

将select第一性别和头衔分别。

xml 的根本问题在于它的结构:除了顺序之外,没有迹象表明第一作者一定是第一本书的作者。也完全有可能一位作者会写不止一本书,或者一本书将由不止一位作者撰写。要在特定作者和特定书籍之间建立必要的联系,必须使用不同的结构。

这似乎是一个分组问题。由于您在评论中说过 I have only the possibilities of XSLT 1.0 您可以使用 xsl:key/key() 执行查找。

举个例子。我根据 id 元素创建了所有 author 元素的键。

这将允许我使用 author_id 元素从 book 访问 author

XML 输入

<root>
    <author>
        <id>MM</id>
        <gender>M</gender>
        <name>Max Mustermann</name>
    </author>
    <author>
        <id>JD</id>
        <gender>F</gender>
        <name>Joanne Doe</name>
    </author>
    <book>
        <author_id>MM</author_id>
        <title>Wie werde ich reich?</title>
        <isbn>123456</isbn>
    </book>
    <book>
        <author_id>JD</author_id>
        <title>Boring jokes</title>
        <isbn>4353442</isbn>
    </book>
</root>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="authors" match="author" use="id"/>

  <xsl:template match="/*">
    <xsl:apply-templates select="book"/>
  </xsl:template>

  <xsl:template match="book">
    <xsl:if test="not(position()=1)">
      <xsl:text>&#xA;</xsl:text>
    </xsl:if>
    <xsl:value-of select="concat('TITLE: ',title,
      ' GENDER: ',key('authors',author_id)/gender)"/>
  </xsl:template>

</xsl:stylesheet>

输出

TITLE: Wie werde ich reich? GENDER: M
TITLE: Boring jokes GENDER: F

Fiddle: http://xsltfiddle.liberty-development.net/6r5Gh48