0
所以我想在使用XSL格式化日期之前通過Web服務發送日期,但是我也想運行檢查,如果DOB字段爲空,它仍然爲空,如果它有一個日期,它會繼續格式化日期。使用XSL檢查XML中的元素
現在我已經得到了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>[email protected]</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>[email protected]</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>
但現在剝離該DOB的XML
我如何能做到這一點有什麼建議?
感謝您的快速回復! 這幾乎涵蓋了我需要的東西 - 雖然如果日期爲空白,仍然存在問題,則將標記錯誤 - 但是從XML中我可以看到DOB沒有被剝離。認爲我需要在DOB中應用一些邏輯到WS中。應用邏輯,如果DOB爲空,則不包含在WS中。 – user1800502