XSLT条件语句选项



为了更多地了解XSLT的可能性,我想知道使用另一种方法编写条件代码是否更好。

它只是在第一个实例中查找href,如果存在href输入,则带有链接的相关图像将显示+ alt标记输出。如果没有href输入,则图像本身将显示+ alt标签输出。

它可以很好地用于特定目的,尽管看起来和感觉有点笨拙。

所以我想知道是否有一种更干净或更聪明的方法来达到这个结果。

如有任何建议,不胜感激。

谢谢,ozmo

无论如何,这是我的杰作…

      <!-- 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

请注意,您的锚定部分将通过默认复制规则自行发生。

完全可以编写没有任何条件指令的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>

您的问题可能主要根据开发人员的意见得到回答。这在SO上不是一件好事。如果我们谈论的是性能或可维护性,就不会/不可能有那么多基于意见的东西。如果它执行得更快,它就是更快!

通过课本"XSLT 2.0和XPATH 2.0 4 "学习。"the Edition"由wrox由可爱的Michael Kay先生出版,您可以遵循以下设计模式:

填表样式表

具有HTML的外观和感觉,但没有使用XSLT的全部功能。该文档主要是html加上一些xslt-tag来获得动态内容,例如<xsl:value-of ..你在特定项目上的风格。

导航样式表

除了填空,它还指向"编程"。在命名模板中外包任务,例如<xsl:template name="renderImage"

基于规则的样式表

您的主要焦点是将xml转换为输出目标。它可以从纯文本到针对任何模式的xml验证再到json验证。您主要编写像<xsl:template match="img"<xsl:template match="a"..这样的模板。创建由<xsl:apply-templates />语句调用的规则集。与其说命令xslt处理程序像"现在调用这个命名模板,然后调用这个命名模板",不如说查找这个节点(-set)的规则并查看那里定义了什么规则

计算样式表

让我们变得复杂一些,充分利用XSLT的强大功能,编写函数,将源树重新排序到目标树,创建节点,并使用多个源文件和目标文件完成此操作。现在您必须了解函数式编程的概念,并超越视界。

最新更新