在 XSLT 中为子元素添加多个条件

Add multiple conditions in XSLT for a child element

我需要将此 XML 格式化为 XSLT。我的示例不适用于第二种情况。当我有多个条件时,我的任务是显示图片("$isMissingImg = 'false' and $brand = 'White T-shirt'")

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<books>
  <book>
      <a>
      <ItemFields>
        <ItemMainCategory>White T-shirt</ItemMainCategory>
      </ItemFields>
    </a>
  </book>
</books>

这是我的 XSLT:

 <xsl:variable name="isMissingImg">
        <xsl:call-template name="isMissingImage">
          <xsl:with-param name="imgUrl" select="$imgSrc"/>
        </xsl:call-template>
      </xsl:variable>

      <div class="logo">
        <xsl:variable name="brand" select="books/book/a/ItemFields/ItemMainCategory"/>
        <xsl:if test="$isMissingImg = 'false'">
            <h2>Hello</h2> <!-- this condition is working--> 
            <xsl:if test="$brand = 'White T-shirt'"> <!-- this condition is NOT working--> 
          <xsl:element name="img"> 
        <xsl:attribute name="src">  
        <xsl:text>https://example.com/images/black.png</xsl:text> 
        </xsl:attribute> 
        <xsl:attribute name='border'>0</xsl:attribute> 
        </xsl:element> 
        </xsl:if>
                </xsl:if>   

            </div>

我测试了你的示例,如果你将 brand 路径设置为绝对路径,它似乎可以工作:

<div class="logo">
    <xsl:variable name="brand" select="/books/book/a/ItemFields/ItemMainCategory"/>
    <xsl:if test="$isMissingImg = 'false'">
        <h2>Hello</h2> <!-- this condition is working--> 
        <xsl:if test="$brand = 'White T-shirt'"> <!-- this condition is NOT working--> 
            <xsl:element name="img"> 
                <xsl:attribute name="src">  
                    <xsl:text>https://example.com/images/black.png</xsl:text> 
                </xsl:attribute> 
                <xsl:attribute name='border'>0</xsl:attribute> 
            </xsl:element> 
        </xsl:if>
    </xsl:if>   
  </div>

顺便说一句,您可以像这样简化此代码:

<div class="logo">
    <xsl:variable name="brand" select="/books/book/a/ItemFields/ItemMainCategory"/>
    <xsl:if test="$isMissingImg = 'false'">
        <h2>Hello</h2> 
        <xsl:if test="$brand = 'White T-shirt'">
            <img src="https://example.com/images/black.png" border="0" />
        </xsl:if>
    </xsl:if>   
</div>

你应该照顾 <xsl:if test="$isMissingImg = 'false'">。它检查 $isMissingImg 是否有字符串值 "false" 和 而不是 布尔值 false。请记住这一点。