Xml 如何将一系列图像转换为幻灯片,将单个图像转换为内联图像?
我需要Xml 如何将一系列图像转换为幻灯片,将单个图像转换为内联图像?,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我需要xslt将使用标记的一系列图像视为幻灯片,将单个图像视为内联图像。我有逻辑来查找被标记包围的,但是扩展它来处理所有非标记是很乏味的。如何编写某种在XSLT1.0中工作的not(p)逻辑 以下是xslt(我删除了完整的调用模板代码,因为它并不重要): 以下是一些示例xml: <?xml version="1.0" encoding="utf-8"?> <message> <article xml:lang="en-US"> &l
xslt
将使用
标记的一系列图像视为幻灯片,将单个图像视为内联图像。我有逻辑来查找被
标记包围的
,但是扩展它来处理所有非
标记是很乏味的。如何编写某种在XSLT1.0中工作的not(p)
逻辑
以下是xslt(我删除了完整的调用模板代码,因为它并不重要):
以下是一些示例xml:
<?xml version="1.0" encoding="utf-8"?>
<message>
<article xml:lang="en-US">
<body>
<h2>What a Chicken!</h2>
<media>
<mediaReference refid="BA_chicken1.jpg"/>
</media>
<ol>
<li>
<p>Place chicken breast side down.</p>
</li>
</ol>
<media>
<mediaReference refid="BA_chicken2.jpg"/>
</media>
<media>
<mediaReference refid="BA_chicken3.jpg"/>
</media>
<p>More about chickens</p>
<media>
<mediaReference refid="BA_chicken4.jpg"/>
</media>
<p>The End</p>
</body>
</article>
</message>
真是一只鸡!
将鸡胸肉面朝下
更多关于鸡的信息
结局
正确的输出如下所示:
<h2>What a Chicken!</h2>
<img src="BA_chicken1.jpg">
<ol><li>
<p>Place chicken breast side down.</p>
</li></ol>
<div class="slideshow">
<img src="BA_chicken2.jpg">
<img src="BA_chicken3.jpg">
</div>
<p>More about chickens</p>
<img src="BA_chicken4.jpg">
<p>The End</p>
真是只鸡!
将鸡胸肉面朝下
更多关于鸡的信息
结局
也许您会从更好地使用匹配模板的方法中受益。如果从标识模板开始,您将有单独的模板来匹配各种情况,例如它是个人还是在组中,对于连续的模板,您有一个递归模板来一次处理一个连续的元素
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:pam="pam">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<!-- Ignore media elements with direct preceding siblings as these are processed elsewhere -->
<xsl:template match="pam:media[preceding-sibling::*[1][self::pam:media]]" priority="2" />
<!-- First of a group -->
<xsl:template match="pam:media[following-sibling::*[1][self::pam:media]]">
<div class="image">
<xsl:apply-templates select="self::*" mode="image" />
</div>
</xsl:template>
<!-- Individual -->
<xsl:template match="pam:media">
<xsl:apply-templates select="self::*" mode="image" />
</xsl:template>
<!-- Template that actually creates the image -->
<xsl:template match="pam:media" mode="image">
<img src="{pam:mediaReference/@pam:refid}" />
<xsl:apply-templates select="following-sibling::*[1][self::pam:media]" mode="image" />
</xsl:template>
<!-- Identity Template -->
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我注意到,XSLT似乎可以区分组中的第一个和最后一个元素,尽管输出XML没有显示这一点。调整此答案以处理此问题的一种方法是修改“图像”模式模板以保持运行计数,以允许其检查是否是组中的第一个或最后一个节点
例如,此更新的模板在第一个和最后一个元素上添加了不同的类:
<xsl:template match="pam:media" mode="image">
<xsl:param name="position" select="1" />
<xsl:variable name="nextElement" select="following-sibling::*[1][self::pam:media]" />
<img src="{pam:mediaReference/@pam:refid}">
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="$position = 1 and $nextElement">first image</xsl:when>
<xsl:when test="$position > 1 and not($nextElement)">last image</xsl:when>
<xsl:otherwise>image</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</img>
<xsl:apply-templates select="$nextElement" mode="image">
<xsl:with-param name="position" select="$position + 1" />
</xsl:apply-templates>
</xsl:template>
第一图像
最后图像
形象
图像的逻辑是什么?除了第一次进入幻灯片放映区外,其他都是吗?此外,命名空间pam:
必须在使用之前声明。您可能在发布示例时忽略了这一点。pam命名空间不在示例中,但对匹配并不重要。我需要将每组两个以上的图像制作成幻灯片,并将单个图像保留为内联图像。我确实需要跟踪组的开始和结束,以便在幻灯片的开头和结尾写下正确的内容。我从xslt中删除了一些节点编写,因为它没有改变逻辑,并使许多样板文件可以阅读。非常感谢您指出,我可以使用变量来存储有关上一个和下一个元素的信息。
<xsl:template match="pam:media" mode="image">
<xsl:param name="position" select="1" />
<xsl:variable name="nextElement" select="following-sibling::*[1][self::pam:media]" />
<img src="{pam:mediaReference/@pam:refid}">
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="$position = 1 and $nextElement">first image</xsl:when>
<xsl:when test="$position > 1 and not($nextElement)">last image</xsl:when>
<xsl:otherwise>image</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</img>
<xsl:apply-templates select="$nextElement" mode="image">
<xsl:with-param name="position" select="$position + 1" />
</xsl:apply-templates>
</xsl:template>