1

我正在使用 XML 编辑器 19.1、Saxon PE 9.7。

对于每个选定div的 ,我希望在graphic/@url每个<surface>if surface/@xml:id=之后显示一个div/@facs

XSL

 <xsl:for-each select="descendant-or-self::div3[@type='col']/div4[@n]">
  <xsl:variable name="div4tablet" select="@facs"/>
   <xsl:choose>
    <xsl:when test="translate(.[@n]/$div4tablet, '#', '') = preceding::facsimile/surfaceGrp[@type='tablet']/surface[@n]/@xml:id">
     <xsl:value-of select=""/> <!-- DISPLAY graphic/@url that follows facsimile/surfaceGrp/surface -->
    </xsl:when>
    <xsl:otherwise/>
   </xsl:choose>
  [....]
 </xsl:for-each> 

TEI例子

 <facsimile>     
  <surfaceGrp n="1" type="tablet">
   <surface n="1.1" xml:id="ktu1-2_i_1_to_10_img">
    <graphic url="../img/KTU-1-2-1-10-recto.jpg"/>
    <zone xml:id=""/>
    <zone xml:id=""/>
   </surface>
    <surface n="1.2" xml:id="ktu1-2_i_10_to_30_img">
    <graphic url="../img/KTU-1-2-10-30-recto.jpg"/>
    <zone xml:id=""/>
   </surface>
   [...]
  </surfaceGrp>
  <surfaceGrp n="2">
  [...]
  </surfaceGrp>
 </facsimile>


 <text>
  [...]
  <div3 type="col">
   <div4 n="1.2.1-10" xml:id="ktu1-2_i_1_to_10" facs="#ktu1-2_i_1_to_10_img">
    [...]
   </div4>
   <div4 n="1.2.10-30" xml:id="ktu1-2_i_10_to_30" facs="#ktu1-2_i_10_to_30_img">
    [...]
   </div4>
  </div3>
 </text> 

我试过<xsl:value-of select="preceding::facsimile/surfaceGrp[@type='tablet']/surface[@n, @xml:id]/graphic/@url"/>了,但它显示了所有graphic/@url,而不仅仅是下面的一个fascsimile/surfaceGrp/surface。所以我的问题是:如何只显示surface/graphic/@url每个div3[@type='col']/div4[@n]

在此先感谢您的帮助。

4

2 回答 2

3

你应该使用xsl:key这种类型的问题。

首先,我们必须为目标节点声明一个键

<xsl:key name="kSurface" match="surface" use="concat('#', @xml:id)"/>

注意concat这里使用的函数,一个 # 被添加到 xml:id 以便键显示为:

#ktu1-2_i_1_to_10_img
#ktu1-2_i_10_to_30_img

现在在这个循环中:

<xsl:for-each select="descendant-or-self::div3[@type='col']/div4[@n]">

我们可以通过以下方式访问与@facs属性匹配的键:

 <xsl:value-of select="key('kSurface', @facs)/graphic/@url"/>

整个样式表如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">

    <xsl:output omit-xml-declaration="yes"/>

    <xsl:key name="kSurface" match="surface" use="concat('#', @xml:id)"/>

    <xsl:template match="/">
        <xsl:for-each select="descendant-or-self::div3[@type='col']/div4[@n]">
            <xsl:value-of select="key('kSurface', @facs)/graphic/@url"/>
            <xsl:text>&#xA;</xsl:text>
        </xsl:for-each> 
    </xsl:template>

</xsl:stylesheet>

在这里看到它的作用。

于 2018-04-30T07:11:03.527 回答
3

当您使用 XSLT 2 或 3 并且元素具有xml:id您甚至不需要键但可以使用该id功能的属性时:

  <xsl:template match="div4">
      <div>
          <xsl:value-of select="id(substring(@facs, 2))/graphic/@url"/>
      </div>
  </xsl:template>

我将使用id放入与元素匹配的模板中,div4但您当然可以在for-each选择这些元素时以相同的方式使用它。

在https://xsltfiddle.liberty-development.net/bdxtpR查看一个最小但完整的示例。

于 2018-04-30T07:14:34.183 回答