0

我需要从属性中读取图像的路径。包含此属性的元素的路径仅由另一个元素的另一个属性引用。也就是说,我在一个元素中有一个 ID,该 ID 引用另一个 ID,该 ID 具有我想要的路径的属性所在的元素。我想将路径用作 html-tag 中的属性。

xml 源

<root>
     <a lot of nodes>
       <relation id="path_1" path="path/to/image1">
       <relation id="path_2" path="path/to/image2">
       ...
     <more nodes>
       <reference path="path_1">
       <reference path="path_2>
       ...
</root>

所需的输出(xslt 片段,类似这样的东西)

<xsl:template match="path/to/reference">
  <img src="{@path}>
  <xsl:apply-templates>
</xsl:template>

所需的输出(html片段)

<img src="path/to/image1>
...
<img src="path/to/image2>

如何使用元素“reference”中的 ID 从元素“relation”中读取 ID 的值?

4

2 回答 2

1

考虑使用xsl:keyhere (这必须作为 的直接子级放置在您的样式表中xsl:stylesheet):

<xsl:key name="relations" match="relation" use="@id" />

然后,在你的模板匹配reference中,你可以这样做

<xsl:template match="reference">
   <img src="{key('relations', @path)/@path}" />
</xsl:template>
于 2018-04-25T08:59:09.533 回答
0

可以满足您需求的模板可能是

<xsl:template match="relation[@id = ../reference/@path]">
  <img src="{@path}">
    <xsl:apply-templates />
  </img>
</xsl:template> 

它的输出是:

<img src="path/to/image1"/>
<img src="path/to/image2"/>

反过来说是:

<xsl:template match="reference[@path = ../relation/@id]">
  <img src="{../relation/@path}">
    <xsl:apply-templates />
  </img>
</xsl:template>  

产生相同的输出。

于 2018-04-24T20:10:12.707 回答