为了更多地了解XSLT的可能性,我想知道这是否是使用不同方法编写此条件代码的更好方法。
它只是在第一个实例中查找href,如果存在href输入,则带有链接的关联图像将显示+ alt标记输出。如果没有href输入,则图像本身将显示+ alt标记输出。
虽然看起来感觉有点笨重,但它在特定用途下效果很好。
所以我想知道是否有更清洁或更聪明的方法来实现结果。
非常感谢任何建议。
谢谢, Ozmo的
Anyhoo,这是我的主人......
<!-- SUPPORTING IMAGE HREF CONDITIONAL -->
<xsl:choose>
<xsl:when test="SupportingImageLink/a/@href !=''">
<tr>
<td>
<a href="{SupportingImageLink/a/@href}">
<img src="{SupportingImage/img/@src}" width="680" alt="{SupportingImage/img/@alt}" style="border: 0;width: 100%;max-width: 680px;" class="center-on-narrow"></img>
</a>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr>
<td>
<img src="{SupportingImage/img/@src}" width="680" alt="{SupportingImage/img/@alt}" style="border: 0;width: 100%;max-width: 680px;" class="center-on-narrow"></img>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
<!-- SUPPORTING IMAGE HREF CONDITIONAL : END -->
根据要求,这是剥离的XML ...
<root>
<Title>New layout test</Title>
<Edition>Octovember 2019</Edition>
<Notification>
<Body>Warning Warning Warning Will Robinson!! Aliens Aliens Aliens everywhere!</Body>
</Notification>
<Introduction>
<Heading>Squids attack!</Heading>
<Body>Ugh tacos artisan, single-origin coffee jianbing hoodie skateboard. 90's unicorn next level fixie. Glossier coloring book drinking vinegar, health goth flexitarian activated charcoal yuccie hexagon whatever normcore bushwick ethical mustache plaid lyft. Chicharrones edison bulb vinyl disrupt tbh glossier, marfa mumblecore four loko +1 leggings.</Body>
</Introduction>
<Section>
<Heading>Just in - Cyborg bears attacking!</Heading>
<Structure>3</Structure>
<SupportingImage>
<img src="/uploadedImages/dev/robots.png?n=3082" alt="Will Robinson" title="Will Robinson" style="width: 680px; height: 283px;" align="left" width="680" height="283" />
</SupportingImage>
<SupportingImageLink>
<a href="http://www.squids-attack/cyb-bears.html">AAARRRRGGGGHHHH!!!</a>
</SupportingImageLink>
<Body>Ugh tacos artisan, single-origin coffee jianbing hoodie skateboard. 90's unicorn next level fixie. Glossier coloring book drinking vinegar, health goth flexitarian activated charcoal yuccie hexagon whatever normcore bushwick ethical mustache plaid lyft. Chicharrones edison bulb vinyl disrupt tbh glossier, marfa mumblecore four loko +1 leggings. Knausgaard af YOLO, direct trade drinking vinegar try-hard williamsburg roof party asymmetrical snackwave waistcoat. Venmo food truck next level raw denim, pabst photo booth quinoa chambray art party hot chicken cliche tote bag polaroid direct trade whatever. Shabby chic lomo locavore slow-carb leggings.</Body>
<Button>More information</Button>
</Section>
</root>
答案 0 :(得分:1)
我会把它写成模板:
<xsl:template match="/SupportingImageLink/img">
<img src="@src" alt="@alt" width="680" .../>
</xsl:template>
和
<tr>
<td>
<xsl:apply-templates select="SupportingImageLink/node()"/>
</td>
</tr>
E&amp; OE
请注意,您的锚点部分将通过默认的复制规则自行发生。
答案 1 :(得分:0)
您的问题可能主要基于开发人员的意见来回答。这对SO来说不是一件好事。如果我们谈论的是性能或可维护性,那么就不会有那么多基于意见的东西。如果它表现得更快,那就更快了!
本书&#34; XSLT 2.0和XPATH 2.0第4版&#34;由lovly Michael Kay先生发布的wrox,你可以遵循设计模式:
填写空白样式表
HTML的外观和感觉并没有使用XSLT的全部功能。该文档主要是html,另外还有一些xslt-tag,用于通过例如xslt-tag获取动态内容。 <xsl:value-of ..
您对该特定项目的风格。
导航样式表
除了填空之外,它还会进入&#34;编程&#34;的方向。在命名模板中外包todos,例如<xsl:template name="renderImage"
。做更多的魔术,而不仅仅是输出一个元素的值。
基于规则的样式表
您的主要目标是将xml转换为输出目标。它可以从纯文本到xml验证,从任何模式到json。您主要编写<xsl:template match="img"
和<xsl:template match="a"..
等模板。您可以创建由<xsl:apply-templates />
- 语句调用的规则集。你宁愿说寻找这个节点的规则(-set)并查看那里定义了什么规则而不是命令xslt处理器像&#34;现在调用这个命名模板然后调用这个命名模板&#34; 。
计算样式表
让我们变得复杂并使用XSLT的全部功能并编写函数,将源树重新排序到目标树,创建节点并使用多个源文件和目标文件执行此操作。现在你必须理解函数式编程的概念并超越视野。
答案 2 :(得分:0)
完全可以在没有任何条件指令的情况下编写XSLT代码。
实际上这是推荐的DRY练习!
<xsl:template match="Section/SupportingImageLink">
<tr>
<td>
<xsl:apply-templates select="a[@href !='']"/>
<xsl:apply-templates select="self::*[not(a[@href !=''])]" mode="getImage"/>
</td>
</tr>
</xsl:template>
<xsl:template match="SupportingImageLink/a[@href !='']/text()">
<xsl:apply-templates select="." mode="getImage"/>
</xsl:template>
<xsl:template match="node()" mode="getImage">
<xsl:param name="pImg" select="ancestor::Section[1]/SupportingImage/img"/>
<img src="{$pImg/@src}" width="680"
alt="{$pImg/@alt}" style="border: 0;width: 100%;max-width: 680px;"
class="center-on-narrow"></img>
</xsl:template>
这是一个完整的转换,在最基本的XSLT设计模式的基础上实现:使用和覆盖身份规则:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Section/SupportingImageLink">
<tr>
<td>
<xsl:apply-templates select="a[@href !='']"/>
<xsl:apply-templates select="self::*[not(a[@href !=''])]" mode="getImage"/>
</td>
</tr>
</xsl:template>
<xsl:template match="SupportingImageLink/a[@href !='']/text()">
<xsl:apply-templates select="." mode="getImage"/>
</xsl:template>
<xsl:template match="node()" mode="getImage">
<xsl:param name="pImg" select="ancestor::Section[1]/SupportingImage/img"/>
<img src="{$pImg/@src}" width="680"
alt="{$pImg/@alt}" style="border: 0;width: 100%;max-width: 680px;"
class="center-on-narrow"></img>
</xsl:template>
</xsl:stylesheet>
对以下XML文档应用此转换时(提供的文档包含一个没有SupportingImageLink
属性链接的其他href
元素:
<root>
<Title>New layout test</Title>
<Edition>Octovember 2019</Edition>
<Notification>
<Body>Warning Warning Warning Will Robinson!! Aliens Aliens Aliens everywhere!</Body>
</Notification>
<Introduction>
<Heading>Squids attack!</Heading>
<Body>Ugh tacos artisan, single-origin coffee jianbing hoodie skateboard.
90's unicorn next level fixie. Glossier coloring book drinking vinegar,
health goth flexitarian activated charcoal yuccie hexagon whatever
normcore bushwick ethical mustache plaid lyft. Chicharrones edison
bulb vinyl disrupt tbh glossier, marfa mumblecore four loko +1 leggings.</Body>
</Introduction>
<Section>
<Heading>Just in - Cyborg bears attacking!</Heading>
<Structure>3</Structure>
<SupportingImage>
<img src="/uploadedImages/dev/robots.png?n=3082" alt="Will Robinson"
title="Will Robinson" style="width: 680px; height: 283px;"
align="left" width="680" height="283" />
</SupportingImage>
<SupportingImageLink>
<a href="http://www.squids-attack/cyb-bears.html">AAARRRRGGGGHHHH!!!</a>
</SupportingImageLink>
<SupportingImageLink>
<a>AAARRRRGGGGHHHH!!!</a>
</SupportingImageLink>
<Body>Ugh tacos artisan, single-origin coffee jianbing hoodie skateboard.
90's unicorn next level fixie. Glossier coloring book drinking vinegar,
health goth flexitarian activated charcoal yuccie hexagon whatever normcore
bushwick ethical mustache plaid lyft. Chicharrones edison bulb vinyl
disrupt tbh glossier, marfa mumblecore four loko +1 leggings. Knausgaard
af YOLO, direct trade drinking vinegar try-hard williamsburg roof party
asymmetrical snackwave waistcoat. Venmo food truck next level raw denim,
pabst photo booth quinoa chambray art party hot chicken cliche tote bag
polaroid direct trade whatever. Shabby chic lomo locavore slow-carb leggings.</Body>
<Button>More information</Button>
</Section>
</root>
正如我们所见,生成了正确的输出:
<root>
<Title>New layout test</Title>
<Edition>Octovember 2019</Edition>
<Notification>
<Body>Warning Warning Warning Will Robinson!! Aliens Aliens Aliens everywhere!</Body>
</Notification>
<Introduction>
<Heading>Squids attack!</Heading>
<Body>Ugh tacos artisan, single-origin coffee jianbing hoodie skateboard.
90's unicorn next level fixie. Glossier coloring book drinking vinegar,
health goth flexitarian activated charcoal yuccie hexagon whatever
normcore bushwick ethical mustache plaid lyft. Chicharrones edison
bulb vinyl disrupt tbh glossier, marfa mumblecore four loko +1 leggings.</Body>
</Introduction>
<Section>
<Heading>Just in - Cyborg bears attacking!</Heading>
<Structure>3</Structure>
<SupportingImage>
<img src="/uploadedImages/dev/robots.png?n=3082" alt="Will Robinson"
title="Will Robinson"
style="width: 680px; height: 283px;"
align="left"
width="680"
height="283"/>
</SupportingImage>
<tr>
<td>
<a href="http://www.squids-attack/cyb-bears.html">
<img src="/uploadedImages/dev/robots.png?n=3082" width="680" alt="Will Robinson"
style="border: 0;width: 100%;max-width: 680px;"
class="center-on-narrow"/>
</a>
</td>
</tr>
<tr>
<td>
<img src="/uploadedImages/dev/robots.png?n=3082" width="680" alt="Will Robinson"
style="border: 0;width: 100%;max-width: 680px;"
class="center-on-narrow"/>
</td>
</tr>
<Body>Ugh tacos artisan, single-origin coffee jianbing hoodie skateboard.
90's unicorn next level fixie. Glossier coloring book drinking vinegar,
health goth flexitarian activated charcoal yuccie hexagon whatever normcore
bushwick ethical mustache plaid lyft. Chicharrones edison bulb vinyl
disrupt tbh glossier, marfa mumblecore four loko +1 leggings. Knausgaard
af YOLO, direct trade drinking vinegar try-hard williamsburg roof party
asymmetrical snackwave waistcoat. Venmo food truck next level raw denim,
pabst photo booth quinoa chambray art party hot chicken cliche tote bag
polaroid direct trade whatever. Shabby chic lomo locavore slow-carb leggings.</Body>
<Button>More information</Button>
</Section>
</root>