Xml 使用<;xsl:when>;及<;xsl:choose>;在文件中

Xml 使用<;xsl:when>;及<;xsl:choose>;在文件中,xml,xslt,Xml,Xslt,我对XML相当陌生,如果它不符合标准,我深表歉意 下面是我的代码,它似乎不起作用。我想我知道为什么。我试图将其设置为,如果有shipID,则发货地址应显示在表/div上,但如果有联系人号码,则应显示。我认为这可能与“shipID”有关,因为它被称为“小于”,但我已经制作了shipID文本,而不是数字。有没有一种方法可以基本上说“如果有一个shipID,那么显示这个”而不是“如果有一个船号小于” 干杯 <tr style= "color:white; background:yello

我对XML相当陌生,如果它不符合标准,我深表歉意

下面是我的代码,它似乎不起作用。我想我知道为什么。我试图将其设置为,如果有shipID,则发货地址应显示在表/div上,但如果有联系人号码,则应显示。我认为这可能与“shipID”有关,因为它被称为“小于”,但我已经制作了shipID文本,而不是数字。有没有一种方法可以基本上说“如果有一个shipID,那么显示这个”而不是“如果有一个船号小于”

干杯

    <tr style= "color:white; background:yellow;">
            <th> header 1</th>
            <th> header 2</th>
            <th> header 3</th>
      </tr>
        <xsl:for-each select="shipping">
          <xsl:choose>
            <xsl:when test="shipID &lt; '2'">
              <tr style="color:black;">
                <td>
                  <xsl:value-of select="header 1"/>
                </td>
                <td>
                  <xsl:value-of select="header 2"/>
                </td>
                <td>
                  <xsl:value-of select="header 3"/>
                </td>
                                  </tr>
            </xsl:when>

             <tr style= "color:white; background:yellow;">
            <th> contact 1</th>
            <th> contact 2</th>
            <th> contact 3</th>
      </tr> <xsl:otherwise>
      <xsl:for-each select="contacts">
        <tr style="color:black;">
          <td>
            <xsl:value-of select="contact1"/>
          </td>
          <td>
            <xsl:value-of select="Contact2"/>
          </td>
          <td>
            <xsl:value-of select="Contact3"/>
          </td>
        </tr>
      </xsl:for-each>
    </xsl:otherwise>
    </xsl:choose>

标题1
标题2
标题3
联系人1
联系人2
联系人3
有没有一种基本的方式可以说“如果有shipID,那么展示这个”

当然有:

<xsl:when test="shipID">
    <!-- some code -->
</xsl:when>


当元素
shipID
作为当前节点的子元素存在时(在您的示例中为
shipping
),将应用
some code
。请注意“存在”“这只是意味着它不一定有值。

XSLT的默认行为是自己导航XML,因此很少需要为每个XML显式地编写循环

如果没有看到XML和代码的其余部分,就不可能给出完整的解决方案,我也不想这样做,因为我试图鼓励您以不同的方式思考XSLT

如果您希望每个shipID都发生一些事情,而每个联系人都会发生其他事情,那么在没有进一步信息的情况下,最简单的方法是编写一个模板规则,当在输入树中遇到这些节点时,该规则将触发。像这样

<xsl:template match="shipID">
  ..do the shipID stuff
</xsl:template>

<xsl:template match="contacts">
  ..do the contacts stuff
</xsl:template>

…做些shipID的事
…做联系人之类的事
如果将模板应用于其父级或允许默认XSLT处理处理其父级,则这些规则将触发(如果必要)

如果您花时间去理解内置默认规则的作用,那么XSLT编程就容易多了,因为您最终不会重新实现本来可以为您做的事情。在学习这些内容的过程中,您可能需要学习应用模板的功能,但学习这两个概念将使您对XSLT的大部分操作变得相对简单。

请展示一个示例。那么,你的问题将是。