[英]JSON output using timing logic in XSLT 1.0
I'm trying to read messages in a XML and output as JSON. 我正在尝试用XML读取消息并输出为JSON。 Each message also has a timing logic to show and hide the message. 每条消息还有一个定时逻辑来显示和隐藏消息。
Below is my XML with each message having start and end date/time 下面是我的XML,每条消息都有开始和结束日期/时间
<activeMessage>
<message>
<messageText>test message 1</messageText>
<displayScheduleContainer>
<startDate>17/05/2019</startDate>
<startTimeHrs>12</startTimeHrs>
<startTimeMins>00</startTimeMins>
<noEndDate/>
<endDate>17/05/2019</endDate>
<endTimeHrs>23</endTimeHrs>
<endTimeMins>59</endTimeMins>
</displayScheduleContainer>
</message>
Below is my XSL 下面是我的XSL
<xsl:for-each select="xalan:nodeset($messageData)/activeMessage/message">
<xsl:variable name="messageInDateTime">
<xsl:call-template name="dateLessThanTemplate">
<xsl:with-param name="startDateTime" select="concat(displayScheduleContainer/startDate, ' ', displayScheduleContainer/startTimeHrs, ':', displayScheduleContainer/startTimeMins)" />
<xsl:with-param name="endDateTime" select="concat(displayScheduleContainer/endDate, ' ', displayScheduleContainer/endTimeHrs, ':', displayScheduleContainer/endTimeMins)" />
</xsl:call-template>
</xsl:variable>
<xsl:if test="$messageInDateTime = 'true'">
<xsl:choose>
<xsl:when test="position()=1">
<xsl:call-template name="singleMessageJSON" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="multiMessageJSON" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
As you have added the tag for XSLT 3 I think you can get rid of any Java call and simply use eg 当你为XSLT 3添加标签时,我认为你可以摆脱任何Java调用,只需使用例如
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:function name="mf:get-dateTime" as="xs:dateTime?">
<xsl:param name="date" as="xs:string"/>
<xsl:param name="hours" as="xs:string"/>
<xsl:param name="minutes" as="xs:string"/>
<xsl:try
select="xs:dateTime(replace($date, '([0-9]{2})/([0-9]{2})/([0-9]{4})', '$3-$2-$1') || 'T' || $hours || ':' || $minutes || ':00')">
<xsl:catch select="()"/>
</xsl:try>
</xsl:function>
<xsl:param name="now" select="current-dateTime()"/>
<xsl:output method="json" indent="yes"/>
<xsl:template match="activeMessage">
<xsl:sequence
select="map {
'message' : array {
message[mf:get-dateTime(displayScheduleContainer/startDate, displayScheduleContainer/startTimeHrs, displayScheduleContainer/startTimeMins) lt $now
and
mf:get-dateTime(displayScheduleContainer/endDate, displayScheduleContainer/endTimeHrs, displayScheduleContainer/endTimeMins) gt $now]/messageText[normalize-space()]/string()
}
}"/>
</xsl:template>
</xsl:stylesheet>
in XSLT 3 with Saxon 9.8 or later. 在XSLT 3中使用Saxon 9.8或更高版本。 I am not sure about the exact JSON output format you want but the main advantage of XSLT 3 is that you can construct maps and arrays in XSLT/XPath and they get serialized as JSON if needed without having to worry about outputting commas in the right place. 我不确定你想要的确切JSON输出格式,但XSLT 3的主要优点是你可以在XSLT / XPath中构建地图和数组,如果需要它们可以序列化为JSON,而不必担心在正确的位置输出逗号。
https://xsltfiddle.liberty-development.net/bnnZWx/1 https://xsltfiddle.liberty-development.net/bnnZWx/1
It would probably be better to move the two comparisons of the dates into another function as that predicate then becomes more readable: 将日期的两个比较移动到另一个函数可能会更好,因为该谓词随后变得更具可读性:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:function name="mf:get-dateTime" as="xs:dateTime?">
<xsl:param name="date" as="xs:string"/>
<xsl:param name="hours" as="xs:string"/>
<xsl:param name="minutes" as="xs:string"/>
<xsl:try
select="xs:dateTime(replace($date, '([0-9]{2})/([0-9]{2})/([0-9]{4})', '$3-$2-$1') || 'T' || $hours || ':' || $minutes || ':00')">
<xsl:catch select="()"/>
</xsl:try>
</xsl:function>
<xsl:function name="mf:compare-dates" as="xs:boolean">
<xsl:param name="schedule" as="element(displayScheduleContainer)"/>
<xsl:sequence
select="mf:get-dateTime($schedule/startDate, $schedule/startTimeHrs, $schedule/startTimeMins) lt $now
and
mf:get-dateTime($schedule/endDate, $schedule/endTimeHrs, $schedule/endTimeMins) gt $now"/>
</xsl:function>
<xsl:param name="now" select="current-dateTime()"/>
<xsl:output method="json" indent="yes"/>
<xsl:template match="activeMessage">
<xsl:sequence
select="map {
'message' : array {
message[mf:compare-dates(displayScheduleContainer)]/messageText[normalize-space()]/string()
}
}"/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/bnnZWx/2 https://xsltfiddle.liberty-development.net/bnnZWx/2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.