如何根据条件返回元素?

时间:2017-03-02 12:50:53

标签: xml xslt

我希望更新我的xslt代码,以便在文档中存在但CourtroomMinutes / ActualEndTime 时, HearingDatetimeEnd元素不会返回

xml文档

<?xml version="1.0" encoding="UTF-8"?>
<CourtroomMinutes Op="A">
    <HearingResult Op="E" Word="HELD">Held</HearingResult>
    <ActualStartTime Op="E">11:00 AM</ActualStartTime>
    <ActualCourtResource Op="A">
        <Type Op="A" Word="REF">Referee</Type>
        <Code Op="A" Word="90748M">Mike, Mark</Code>
    </ActualCourtResource>
</CourtroomMinutes>

当前返回的结果是错误的

<HearingDateTimes>
   <HearingDatetimeStart>2017-03-01T11:00:00-06:00</HearingDatetimeStart>
   <HearingDatetimeEnd>2017-03-01T10:00:00-06:00</HearingDatetimeEnd>
</HearingDateTimes>

期望的结果

<HearingDateTimes>
   <HearingDatetimeStart>2017-03-01T11:00:00-06:00</HearingDatetimeStart>
</HearingDateTimes>

要修改的xslt代码

<HearingDateTimes>
    <HearingDatetimeStart>
        <xsl:choose>
            <xsl:when test="string-length(CourtroomMinutes/ActualStartTime)!=0">
                <xsl:value-of select="mscef:formatDateTime(concat(HearingDate, ' ', CourtroomMinutes/ActualStartTime))"/>
            </xsl:when>
            <xsl:when test="string-length(StartTime)!=0">
                <xsl:value-of select="mscef:formatDateTime(concat(HearingDate, ' ', StartTime))"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="mscef:formatDateTime(concat(HearingDate, ' ', CourtSessionBlock/StartTime))"/>
            </xsl:otherwise>
        </xsl:choose>
    </HearingDatetimeStart>
    <HearingDatetimeEnd>
        <xsl:choose>
            <xsl:when test="string-length(CourtroomMinutes/ActualEndTime)!=0">
                <xsl:value-of select="mscef:formatDateTime(concat(HearingDate, ' ', CourtroomMinutes/ActualEndTime))"/>
            </xsl:when>
            <xsl:when test="string-length(EndTime)!=0">
                <xsl:value-of select="mscef:formatDateTime(concat(HearingDate, ' ', EndTime))"/>
            </xsl:when>
        </xsl:choose>
    </HearingDatetimeEnd>
</HearingDateTimes>

1 个答案:

答案 0 :(得分:1)

您可以将当前元素包装在xsl:if

<xsl:if test="CourtroomMinutes/ActualStartTime and not(CourtroomMinutes/ActualEndTime)">
  <HearingDatetimeEnd>
    ....
  </HearingDatetimeEnd>
</xsl:if>

或者你可以将表达式简化为......

<xsl:if test="CourtroomMinutes[ActualStartTime and not(ActualEndTime)]">