xsl:当两个节点相等时,显示第一个节点的子节点

时间:2018-04-30 00:47:16

标签: xslt tei

我正在使用XML Editor 19.1,Saxon P.E 9.7。

对于每个选定的div,如果graphic/@url = <surface>,我希望在每个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]显示 #!/bin/bash nmap -v -p5900 --script=vnc-screenshot-it --script-args vnc-screenshot.quality=30 x.x.x.x

提前感谢您的帮助。

2 个答案:

答案 0 :(得分: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>

在行动here中查看。

答案 1 :(得分: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查看最小但完整的样本。

相关问题