首页 > 解决方案 > 如何在 if 条件下分配固定值

问题描述

我正在使用 XSL 样式表转换几个 XML。

但是,在某些情况下,“日期”字段只有年份而不是完整日期 DD/MM/YYYY。

这里举个例子。

<?xml version="1.0" encoding="UTF-8"?>
<objects>
    <oeu>
        <oeu_archive_exist_construct_timespan type="daterange">
            <from>1999</from>
            <to>2001</to>
        </oeu_archive_exist_construct_timespan>
    </oeu>
</objects>

通过应用以下 XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"
                indent="yes"
                encoding="UTF-8"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates/>
        </root>
    </xsl:template>
    <xsl:template match="@*|node()"
                  mode="copy-no-namespaces">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"
                                 mode="copy-no-namespaces"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="pr:objects/pr:oeu">
        <entry>
            <xsl:element name="construction_time_span">
                <oeu_nc_construction_time_span_from>
                    <xsl:value-of select="pr:oeu_archive_exist_construct_timespan/pr:from"/>
                </oeu_nc_construction_time_span_from>
                <oeu_nc_construction_time_span_to>
                    <xsl:value-of select="pr:oeu_archive_exist_construct_timespan/pr:to"/>
                </oeu_nc_construction_time_span_to>
            </xsl:element>
        </entry>
    </xsl:template>
</xsl:stylesheet>

我有以下输出(快照):

<construction_time_span>
   <oeu_nc_construction_time_span_from>1999</oeu_nc_construction_time_span_from>
   <oeu_nc_construction_time_span_to>2001</oeu_nc_construction_time_span_to>
</construction_time_span>  

在日期只有年份的情况下,我想将固定值 1/1 分配为 DD/MM。

我想要的输出是:

<construction_time_span>
   <oeu_nc_construction_time_span_from>1/1/1999</oeu_nc_construction_time_span_from>
   <oeu_nc_construction_time_span_to>1/1/2001</oeu_nc_construction_time_span_to>
</construction_time_span>  

建议?

问候,

标签: xmlxslt

解决方案


推荐阅读