因此,我想使用 XSL 在通过 Web 服务发送日期之前对其进行格式化,但我还想检查 DOB 字段是否为空白,它是否保持空白,如果其中有日期,它将继续格式化日期。
现在我已经让 XSL 格式化日期 - 但是我似乎无法让条件正常工作,如果它为空则让它为空
通过 XSL 之前的 XML:
<BriefDetails>
<ObjectID>
<flt:ObjectType>C1</flt:ObjectType>
</ObjectID>
</BriefDetails>
<Name>
<Title>Dr</Title>
<Forename>Paul</Forename>
<Surname>Smith</Surname>
<flt:Preferred>true</flt:Preferred>
</Name>
<ContactEmails>
<EmailAddress>mail@mail.com</EmailAddress>
<flt:Preferred>true</flt:Preferred>
</ContactEmails>
<DateOfBirth />
<ContactPostals>
<AddressNumber>34</AddressNumber>
<AddressLine>Hopeless Road</AddressLine>
<AddressLine>Maghera</AddressLine>
<Postcode>BT74 2TY</Postcode>
<flt:Preferred>true</flt:Preferred>
</ContactPostals>
<ContactPhones>
<Number>5545454</Number>
<flt:Preferred>false</flt:Preferred>
</ContactPhones>
<ContactPhones>
<Number>5454545</Number>
<flt:Preferred>false</flt:Preferred>
</ContactPhones>
XSL 之后的 XML:
<BriefDetails>
<ObjectID>
<flt:ObjectType>C1</flt:ObjectType>
</ObjectID>
</BriefDetails>
<Name>
<Title>Dr</Title>
<Forename>Paul</Forename>
<Surname>Smith</Surname>
<flt:Preferred>true</flt:Preferred>
</Name>
<ContactEmails>
<EmailAddress>mail@mail.com</EmailAddress>
<flt:Preferred>true</flt:Preferred>
</ContactEmails>
***<DateOfBirth>--</DateOfBirth>***
<ContactPostals>
<AddressNumber>34</AddressNumber>
<AddressLine>Hopeless Road</AddressLine>
<AddressLine>Maghera</AddressLine>
<Postcode>BT74 2TY</Postcode>
<flt:Preferred>true</flt:Preferred>
</ContactPostals>
<ContactPhones>
<Number>5545454</Number>
<flt:Preferred>false</flt:Preferred>
</ContactPhones>
<ContactPhones>
<Number>5454545</Number>
<flt:Preferred>false</flt:Preferred>
</ContactPhones>
在上面你会看到 DOB 为——
正在使用的 XSL:
<xsl:template match="DateOfBirth">
<xsl:element name="DateOfBirth">
<xsl:call-template name="formatDate">
<xsl:with-param name="dateParam" select="." />
</xsl:call-template>
</xsl:element>
</xsl:template>
<xsl:template name="formatDate">
<xsl:param name="dateParam" />
<!-- input format mm/dd/yyyy or m/d/yyyy -->
<!-- output format yyyy-mm-dd -->
<!-- parse out the day, month and year -->
<xsl:variable name="day">
<xsl:value-of select="substring-before($dateParam,'/')" />
</xsl:variable>
<xsl:variable name="month">
<xsl:value-of select="substring-before(substring-after($dateParam,'/'),'/')" />
</xsl:variable>
<xsl:variable name="year">
<xsl:value-of select="substring-after(substring-after($dateParam,'/'),'/')" />
</xsl:variable>
<!-- now print them out. Pad with 0 where necessary. -->
<xsl:value-of select="$year" />
<xsl:value-of select="'-'" />
<xsl:if test="string-length($month) = 1">
<xsl:value-of select="'0'" />
</xsl:if>
<xsl:value-of select="$month" />
<xsl:value-of select="'-'" />
<xsl:if test="string-length($day) = 1">
<xsl:value-of select="'0'" />
</xsl:if>
<xsl:value-of select="$day" />
</xsl:template>
我尝试添加一个条件:
<xsl:template match="DateOfBirth">
<xsl:for-each select="DateOfBirth">
<xsl:if test="string-length(DateOfBirth) != 0">
<xsl:call-template name="formatDate">
<xsl:with-param name="dateParam" select="DateOfBirth" />
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="formatDate">
<xsl:param name="dateParam" />
<!-- input format mm/dd/yyyy or m/d/yyyy -->
<!-- output format yyyy-mm-dd -->
<!-- parse out the day, month and year -->
<xsl:variable name="day">
<xsl:value-of select="substring-before($dateParam,'/')" />
</xsl:variable>
<xsl:variable name="month">
<xsl:value-of select="substring-before(substring-after($dateParam,'/'),'/')" />
</xsl:variable>
<xsl:variable name="year">
<xsl:value-of select="substring-after(substring-after($dateParam,'/'),'/')" />
</xsl:variable>
<!-- now print them out. Pad with 0 where necessary. -->
<xsl:value-of select="$year" />
<xsl:value-of select="'-'" />
<xsl:if test="string-length($month) = 1">
<xsl:value-of select="'0'" />
</xsl:if>
<xsl:value-of select="$month" />
<xsl:value-of select="'-'" />
<xsl:if test="string-length($day) = 1">
<xsl:value-of select="'0'" />
</xsl:if>
<xsl:value-of select="$day" />
</xsl:template>
但这现在去掉了 XML 中的 DOB
关于如何实现这一目标的任何建议?