将 XML 中的工作日和时间转换为 JSON

Converting Weekdays & Time in XML to JSON

我正在使用 XSLT 1.0,需要将以下 XML 有效负载转换为 JSON:

XML 负载:

 <OpeningTimes>
 <MondayOpen>00:00</MondayOpen>
  <MondayClosed>12:00</MondayClosed>
  <TuesdayOpen>13:00</TuesdayOpen>
  <TuesdayClosed>23:00</TuesdayClosed>
  <WednesdayOpen>00:00</WednesdayOpen>
  <WednesdayClosed>12:00</WednesdayClosed>
  <ThursdayOpen>01:00</ThursdayOpen>
  <ThursdayClosed>12:00</ThursdayClosed>
  <FridayOpen>01:00</FridayOpen>
  <FridayClosed>12:00</FridayClosed>
  <SaturdayOpen>01:00</SaturdayOpen>
  <SaturdayClosed>12:00</SaturdayClosed>
  <SundayOpen>01:00</SundayOpen>
  <SundayClosed>12:00</SundayClosed>
   </OpeningTimes>

预期JSON输出:

{
"opening_hours": {
"open_now": false,
"periods": [
{
"close": {
"day": 0,
"time": "1200"
},
"open": {
"day": 0,
"time": "0000"
}
},
{
"close": {
"day": 1,
"time": "2300"
},
"open": {
"day": 1,
"time": "1300"
}
},
...
...
...
    {
    "close": {
    "day": 6,
    "time": "1200"
    },
    "open": {
    "day": 6,
    "time": "0000"
    }
    }
    ],
    "weekday_text": [
    "Monday: 12:00 AM – 12:00 PM",
    "Tuesday: 1:00 PM – 11:00 PM",
    "Wednesday: 12:00 AM – 12:00 PM",
    "Thursday: 12:00 AM – 12:00 PM",
    "Friday: 12:00 AM – 12:00 PM",
    "Saturday: 12:00 AM – 12:00 PM",
    "Sunday: 12:00 AM – 12:00 PM"
    ]
    }
    }

我尝试在初始部分使用以下方法,但我认为这不是最好的方法:

<xsl:template match="au:MondayOpen">
        <day>
             <xsl:text>0</xsl:text>
        </day>
        <time>
            <xsl:value-of select="." />
        </time>
    </xsl:template>
        <xsl:template match="au:MondayClosed">
        <day>
             <xsl:text>0</xsl:text>
        </day>
        <time>
            <xsl:value-of select="." />
        </time>
    </xsl:template>
        <xsl:template match="au:TuesdayOpen">
        <day>
             <xsl:text>1</xsl:text>
        </day>
        <time>
            <xsl:value-of select="." />
        </time>
    </xsl:template>
        <xsl:template match="au:TuesdayClosed">
        <day>
            <xsl:text>1</xsl:text>
        </day>
        <time>
            <xsl:value-of select="." />
        </time>
    </xsl:template>

等等。

我看到 this post 关于转换时间,我需要在 JSON 的后面部分。请忽略输出中给定的时间值,这只是一个例子。

如果输入总是包含一周中每一天的两个条目,按照给定的顺序,您可以通过执行以下操作来消除代码重复:

<xsl:template match="OpeningTimes">
    <xsl:text>[</xsl:text>
    <xsl:for-each select="*[position() mod 2 = 0]">
        <xsl:variable name="day" select="position() - 1" />
        <xsl:text>{"close":{"day": </xsl:text>
        <xsl:value-of select="$day"/>
        <xsl:text>,"time":"</xsl:text>
        <xsl:value-of select="translate(., ':', '')"/>
        <xsl:text>"},"open":{"day": </xsl:text>
        <xsl:value-of select="$day"/>
        <xsl:text>,"time":"</xsl:text>
        <xsl:value-of select="translate(preceding-sibling::*[1], ':', '')"/>
        <xsl:text>"}}</xsl:text>
        <xsl:if test="position()!=last()">,</xsl:if>
    </xsl:for-each>
    <xsl:text>]</xsl:text>
</xsl:template>

生产:

[{"close":{"day": 0,"time":"1200"},"open":{"day": 0,"time":"0000"}},{"close":{"day": 1,"time":"2300"},"open":{"day": 1,"time":"1300"}},{"close":{"day": 2,"time":"1200"},"open":{"day": 2,"time":"0000"}},{"close":{"day": 3,"time":"1200"},"open":{"day": 3,"time":"0100"}},{"close":{"day": 4,"time":"1200"},"open":{"day": 4,"time":"0100"}},{"close":{"day": 5,"time":"1200"},"open":{"day": 5,"time":"0100"}},{"close":{"day": 6,"time":"1200"},"open":{"day": 6,"time":"0100"}}]

格式化后变为:

[
    {
        "close" : 
        {
            "day" : 0,
            "time" : "1200"
        },
        "open" : 
        {
            "day" : 0,
            "time" : "0000"
        }
    },
    {
        "close" : 
        {
            "day" : 1,
            "time" : "2300"
        },
        "open" : 
        {
            "day" : 1,
            "time" : "1300"
        }
    },
    {
        "close" : 
        {
            "day" : 2,
            "time" : "1200"
        },
        "open" : 
        {
            "day" : 2,
            "time" : "0000"
        }
    },
    {
        "close" : 
        {
            "day" : 3,
            "time" : "1200"
        },
        "open" : 
        {
            "day" : 3,
            "time" : "0100"
        }
    },
    {
        "close" : 
        {
            "day" : 4,
            "time" : "1200"
        },
        "open" : 
        {
            "day" : 4,
            "time" : "0100"
        }
    },
    {
        "close" : 
        {
            "day" : 5,
            "time" : "1200"
        },
        "open" : 
        {
            "day" : 5,
            "time" : "0100"
        }
    },
    {
        "close" : 
        {
            "day" : 6,
            "time" : "1200"
        },
        "open" : 
        {
            "day" : 6,
            "time" : "0100"
        }
    }
]

我建议您针对这可能需要的任何其他任务提出单独的问题。要将时间从 24 小时制转换为 12 小时制,请参阅: