使用Xsl-Key和generate-id()函数

时间:2015-10-09 10:05:56

标签: xslt xslt-1.0

我会在第一个IMG关联第一个ATTACHED_FILENAME和 在第二个IMG第二个附加文件名。

这是我的XML:

<INSTRUCTION_LIST_ITEM>
    <NTC_SD_INSTRUCT>
      <ACTION>Sostituire</ACTION>
      <PLACEMENT>le righe 10 &#xF7; 18 con:</PLACEMENT>
      <DESCRIPTION>
        <P>Il porto &#xC3;&#xA8; protetto da un molo foraneo.</P>
        <P>
          <IMG border="0" hspace="0" alt="" align="baseline" src="C:\Users\l_sturla\Desktop\albany.jpg"/>
        </P>
        <P>Ben visibile da nord &#xC3;&#xA8; il faro della Vittoria.</P>
        <P>
          <IMG border="0" hspace="0" alt="" align="baseline" src="C:\Users\l_sturla\Desktop\Faro vittoria.JPG"/>
        </P>
        <P> </P>
        <P>Mantenersi a distanza di sicurezza.</P>
      </DESCRIPTION>
      <ATTACHMENT_LIST>
        <ATTACHMENT>
          <ATTACHED_FILENAME>albany.jpg</ATTACHED_FILENAME>
        </ATTACHMENT>
        <ATTACHMENT>
          <ATTACHED_FILENAME>Faro vittoria.JPG</ATTACHED_FILENAME>
        </ATTACHMENT>
      </ATTACHMENT_LIST>
    </NTC_SD_INSTRUCT>
  </INSTRUCTION_LIST_ITEM>

我创建了这个XSLT:

<xsl:template match="//IMG">
    <span style="font-style:italic">
        <xsl:choose>
            <xsl:when  test="count(ancestor::DESCRIPTION//IMG) = count(ancestor::DESCRIPTION/following-sibling::ATTACHMENT_LIST/ATTACHMENT/ATTACHED_FILENAME)">
             <img>
                <xsl:attribute name="src">
                    <xsl:value-of select="ancestor::NTC_SD_INSTRUCT/ATTACHMENT_LIST/ATTACHMENT/ATTACHED_FILENAME"/>
                </xsl:attribute>
              </img>
            </xsl:when>             
        </xsl:choose>
    </span>
</xsl:template>

但这总是给出了第一张图片。 ATTACHED_FILENAME标记是属性SRC的参数。

2 个答案:

答案 0 :(得分:1)

尝试

<xsl:template match="IMG">
  <xsl:variable name="counter">
   <xsl:number level="any" from="DESCRIPTION"/>
  </xsl:variable>
  <img src="{(//ATTACHED_FILENAME)[number($counter)]}"/>
</xsl:template>

答案 1 :(得分:0)

如果您定义了一个键<xsl:key name="attachment-by-pos" match="NTC_SD_INSTRUCT/ATTACHMENT_LIST/ATTACHMENT" use="concat(generate-id(ancestor::NTC_SD_INSTRUCT), '|', count(preceding-sibling::ATTACHMENT))"/>,那么您可以使用模板

<xsl:template match="IMG">
    <span style="font-style:italic">
      <img src="{key('attachment-by-pos', concat(generate-id(ancestor::NTC_SD_INSTRUCT), '|', count(../preceding-sibling::*//IMG)))/ATTACHED_FILENAME}"/> 
     </span>
</xsl:template>