【问题标题】:Using conditions in XSL在 XSL 中使用条件
【发布时间】:2014-01-16 21:43:25
【问题描述】:

我正在处理一些 XML 文件,在某些情况下,HName 节点在某些文件中可用,而在其他文件中却没有。这里有两个例子:

Xml #1:

<HSegment>
<Code>ABC</Code> 
</HSegment>

Xml #2:

<HSegment>
<Code>ABC</Code> 
<HName>JW BEACH</HName>  
</HSegment>

我正在尝试使用两个条件解析 xml 文件:

  1. 处理数据字段中的值(如果可用)。
  2. 如果不可用,则插入“NULL”。

我在下面使用的 XSL 代码在这两种情况下都添加了“NULL”:

<Des>
<xsl:choose>
<xsl:when test="//PNR/SList/HSegment/HName='HName'">
</xsl:when>
<xsl:otherwise>
<xsl:text>NULL</xsl:text> 
</xsl:otherwise>
</xsl:choose>
</Des>

提前致谢!

【问题讨论】:

    标签: xml xslt xml-parsing


    【解决方案1】:

    将存在检查修改为如下所示:

    //PNR/SList/HSegment/HName
    

    在上下文中:

    <xsl:choose>
        <xsl:when test="//PNR/SList/HSegment/HName">
            <!-- do whatever -->
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>NULL</xsl:text> 
        </xsl:otherwise>
    </xsl:choose>
    

    【讨论】:

    • 感谢您的意见,但我也尝试过这种方法。这不会解析其中包含 (HName) 节点的 XML 文件。他们完全失败了。
    • 我已经测试过了,效果很好。如果它失败了,那是因为你在问题中没有提到的其他原因。
    猜你喜欢
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 2020-03-04
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多