我必须以这种方式处理文本和图像序列:
在实践中,我将每个IMG关联到相应的ATTACHED_FILENAME,但如果不存在相应的IMG,则ATTACHED_FILENAME在此情况下是警报的外部图像。
这是我的XML:
<NTC_SD_NOTICE>
<PUBLISH_NUMBER>2 caso</PUBLISH_NUMBER>
<PUBLICATION_REF>
<PUBLICATION_SHORT_TITLE>Portolano P8</PUBLICATION_SHORT_TITLE>
<PUBLICATION_EDITION_YEAR>2013</PUBLICATION_EDITION_YEAR>
<PUBLICATION_PAGE>128</PUBLICATION_PAGE>
</PUBLICATION_REF>
<REGION>15-Mar Ligure - Francia (Corsica)</REGION>
<VICINITY>Appartamento tuo</VICINITY>
<SUBJECT>Fotografie</SUBJECT>
<AUTHORITY>Compamare Arzachena</AUTHORITY>
<PROJECT>230/8562</PROJECT>
<INSTRUCTION_LIST>
<INSTRUCTION_LIST_ITEM>
<NTC_SD_INSTRUCT>
<ACTION>Inserire</ACTION>
<PLACEMENT>dopo la riga 1:</PLACEMENT>
<DESCRIPTION>
<P>Dietro al monte si vede una meda cardinale sud.</P>
<P>
<IMG border="0" hspace="0" alt="" align="baseline" src="C:\Users\l_sturla\Desktop\meda.JPG"/> <-- First image
</P>
</DESCRIPTION>
<ATTACHMENT_LIST>
<ATTACHMENT>
<ATTACHED_FILENAME>meda.JPG</ATTACHED_FILENAME> <-- Attribute first image
</ATTACHMENT>
<ATTACHMENT>
<ATTACHED_FILENAME>altro.JPG</ATTACHED_FILENAME> <-- In this case there are no second IMG, this is an external image.
</ATTACHMENT>
</ATTACHMENT_LIST>
</NTC_SD_INSTRUCT>
</INSTRUCTION_LIST_ITEM>
<INSTRUCTION_LIST_ITEM>
<NTC_SD_INSTRUCT>
<ACTION>Sostituire</ACTION>
<PLACEMENT>le righe 34 ÷ 50 con:</PLACEMENT>
<DESCRIPTION>Proseguire per il centro del canale e prestare attenzione al relitto sulla dritta.</DESCRIPTION>
<ATTACHMENT_LIST>
<ATTACHMENT>
<ATTACHED_FILENAME>relitto.JPG</ATTACHED_FILENAME>
</ATTACHMENT>
</ATTACHMENT_LIST>
</NTC_SD_INSTRUCT>
</INSTRUCTION_LIST_ITEM>
</INSTRUCTION_LIST>
</NTC_SD_NOTICE>
这是我的XSLT 1.0:
内部形象的模板:(感谢Martin Honnen表现非常好)
<!-- TEMPLATE PER LE IMMAGINI INTERNE ALL'AVVISO -->
<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>
外部图像模板:(不工作 - 所有与IMG无关的ATTACHED_FILENAME)
<xsl:template name="allegati">
<center><xsl:text>Allegati:</xsl:text></center>
<xsl:for-each select="INSTRUCTION_LIST/INSTRUCTION_LIST_ITEM/NTC_SD_INSTRUCT/ATTACHMENT_LIST/ATTACHMENT/ATTACHED_FILENAME">
<center>
<img src="{key('attachment-by-pos', concat(generate-id(ancestor::NTC_SD_INSTRUCT), '|', count(../preceding-sibling::*//IMG)))/ATTACHED_FILENAME}"/>
</center>
</xsl:for-each>
</xsl:template>
在这种情况下,输出必须是:
Inserire dopo la riga 1:Dietro al monte si vede una meda cardinale sud。
-IMMAGINE MEDA.jpg-
Sostituire le righe 34÷50 con:Proseguire per il centro del canale e prestare attenzione al relitto sulla dritta。
Allegati:
-IMMAGINE altro.jpg-
-IMMAGINE relitto.jpg -
用粗体文本突出显示的此部分不会运行。
最好的问候。
答案 0 :(得分:0)
在上一个问题中,Martin Honnen给出的答案定义了以下关键字,用于在ATTACHMENT
元素中按位置查找NTC_SD_INSTRUCT
元素。
我认为对于您的新要求,您正在颠倒逻辑,因此您还可以定义一个键,以便按位置查找IMG
元素
<xsl:key name="img-by-pos" match="NTC_SD_INSTRUCT/DESCRIPTION/P/IMG" use="concat(generate-id(ancestor::NTC_SD_INSTRUCT), '|', count(../preceding-sibling::*//IMG))"/>
然后,在xsl:for-each
模板的allegati
中,您可以使用此密钥检查它们是否是关联的IMG
尝试使用此模板
<xsl:template name="allegati">
<center><xsl:text>Allegati:</xsl:text></center>
<xsl:for-each select="INSTRUCTION_LIST/INSTRUCTION_LIST_ITEM/NTC_SD_INSTRUCT/ATTACHMENT_LIST/ATTACHMENT
[not(key('img-by-pos', concat(generate-id(ancestor::NTC_SD_INSTRUCT), '|', count(preceding-sibling::ATTACHMENT))))]">
<center>
<img src="{ATTACHED_FILENAME}"/>
</center>
</xsl:for-each>
</xsl:template>