XSLT 条件语句选项
XSLT conditional statement options
为了进一步了解 XSLT 的可能性,我想知道使用不同的方法是否是编写此条件代码的更好方法。
它只是在第一个实例中查找 href,如果存在 href 输入,则带有 link 的关联图像将显示 + alt 标签输出。如果没有 href 输入,只有图像本身会显示 + alt 标签输出。
虽然看起来和感觉起来有点笨重,但它可以很好地满足特定目的。
所以我想知道是否有更清洁或更智能的方法来实现结果。
如有任何建议,我们将不胜感激。
谢谢,
奥兹莫
好吧,这是我的杰作...
<!-- 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>
我会把它写成模板:
<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&OE
请注意,您的锚点部分将通过默认复制规则自行生成。
您的问题可能主要根据开发人员的意见来回答。这对 SO 来说不是一件好事。如果我们谈论的是性能或可维护性,那么't/can不会有那么多基于意见的东西。如果它执行得更快,那就更快!
通过 lovly Mr. Michael Kay 的 wrox 出版的书 "XSLT 2.0 and XPATH 2.0 4.th Edition" 学习,有设计模式你可以遵循:
Fill-in-the-blanks stylesheets
HTML 的外观和感觉,没有使用 XSLT 的全部功能。该文档主要是 html 以及一些 xslt-tag 以通过例如获取动态内容<xsl:value-of ..
你在那个特定项目上的风格。
Navigational Stylesheet
除了填空之外,它还朝着"programming"的方向发展。在命名模板中外包待办事项,例如<xsl:template name="renderImage"
。比仅仅输出一个元素的值更神奇。
Rule-based Stylesheet
您的主要工作重点是将 xml 转换为输出目标。它可以从纯文本到 xml 针对任何模式验证到 json。您主要编写 <xsl:template match="img"
和 <xsl:template match="a"..
等模板。您创建由 <xsl:apply-templates />
语句调用的规则集。您宁愿说 查找此节点(-集)的规则并查看那里定义的规则 而不是 命令 xslt 处理器,如 "do call this named template now and after that call this named template".
Computational Stylesheet
让我们变得更复杂,并使用 XSLT 的全部功能并编写函数,将源树重新排序为目标树,创建节点并使用多个源文件和目标文件执行此操作。现在你要明白函数式编程的概念,超越视界
完全可以在没有任何条件指令的情况下编写 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
元素,该元素没有 link 和 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>
为了进一步了解 XSLT 的可能性,我想知道使用不同的方法是否是编写此条件代码的更好方法。
它只是在第一个实例中查找 href,如果存在 href 输入,则带有 link 的关联图像将显示 + alt 标签输出。如果没有 href 输入,只有图像本身会显示 + alt 标签输出。
虽然看起来和感觉起来有点笨重,但它可以很好地满足特定目的。
所以我想知道是否有更清洁或更智能的方法来实现结果。
如有任何建议,我们将不胜感激。
谢谢, 奥兹莫
好吧,这是我的杰作...
<!-- 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>
我会把它写成模板:
<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&OE
请注意,您的锚点部分将通过默认复制规则自行生成。
您的问题可能主要根据开发人员的意见来回答。这对 SO 来说不是一件好事。如果我们谈论的是性能或可维护性,那么't/can不会有那么多基于意见的东西。如果它执行得更快,那就更快!
通过 lovly Mr. Michael Kay 的 wrox 出版的书 "XSLT 2.0 and XPATH 2.0 4.th Edition" 学习,有设计模式你可以遵循:
Fill-in-the-blanks stylesheets
HTML 的外观和感觉,没有使用 XSLT 的全部功能。该文档主要是 html 以及一些 xslt-tag 以通过例如获取动态内容<xsl:value-of ..
你在那个特定项目上的风格。
Navigational Stylesheet
除了填空之外,它还朝着"programming"的方向发展。在命名模板中外包待办事项,例如<xsl:template name="renderImage"
。比仅仅输出一个元素的值更神奇。
Rule-based Stylesheet
您的主要工作重点是将 xml 转换为输出目标。它可以从纯文本到 xml 针对任何模式验证到 json。您主要编写 <xsl:template match="img"
和 <xsl:template match="a"..
等模板。您创建由 <xsl:apply-templates />
语句调用的规则集。您宁愿说 查找此节点(-集)的规则并查看那里定义的规则 而不是 命令 xslt 处理器,如 "do call this named template now and after that call this named template".
Computational Stylesheet
让我们变得更复杂,并使用 XSLT 的全部功能并编写函数,将源树重新排序为目标树,创建节点并使用多个源文件和目标文件执行此操作。现在你要明白函数式编程的概念,超越视界
完全可以在没有任何条件指令的情况下编写 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
元素,该元素没有 link 和 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>