XSLT转换日期时间为日期格式日期、时间为、格式、XSLT

2023-09-03 13:26:07 作者:我知道我不行#

我想变换日期时间为日期格式YYYY-MM-DD,因为我使用xsd.exe工具的XS:日期数据类型将自动改变成datetime数据类型,因为没有类型.NET框架相匹配的类型为xs:日期完全

但我不能让它的工作。

 <文章>
        <物品>
          <条款ArticleID> 48992< /条款ArticleID>
          < deliverydateasked> 2009-01-29T00:00:00 + 01:00< / deliverydateasked>
        < /条>
        <物品>
          <条款ArticleID> 48993< /条款ArticleID>
          < deliverydateasked> 2009-01-30T00:00:00 + 01:00< / deliverydateasked>
        < /条>
< /物品>
 

试图将XML转换成

 <文章>
        <物品>
          <条款ArticleID> 48992< /条款ArticleID>
          < deliverydateasked> 2009-01-29< / deliverydateasked>
        < /条>
        <物品>
          <条款ArticleID> 48993< /条款ArticleID>
          < deliverydateasked> 2009-01-30< / deliverydateasked>
        < /条>
< /物品>
 

目前我使用的这个XSLT

 < XSL:样式的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform版本=1.0>
< XSL:模板匹配=/>
<文章>
< XSL:申请-模板选择=文章>
< / XSL:申请 - 模板>
            < /物品>
< / XSL:模板>

< XSL:模板名称=FormatDate>

< XSL:参数名称=日期时间/>
< XSL:变量名=约会>
< XSL:选择=串 - 前($日期时间,'T')/&GT价值的;
< / XSL:变量>

< XSL:如果测试=!字符串长度($日期)= 10>
< XSL:选择=$日期时间/&GT价值的;
< / XSL:如果>
< XSL:如果测试=字符串长度($日期)= 10>
< XSL:value-of的选择=$日/>
< / XSL:如果>
< / XSL:模板>

< XSL:模板匹配=文章>
< XSL:呼叫模板名称=FormatDate>
< XSL:with-param中NAME =日期时间选择=deliverydateasked/>
< / XSL:呼叫模板>
< / XSL:模板>
 
excel中日期和时间格式如何转换成只有日期的格式

有谁知道一个良好的XSLT转换。

在此先感谢

我的code的输出结果是

 <文章/>
 

解决方案

由于Stesoc和annakata我想通了 这是code现在我正使用它完美的作品

 < XSL:模板匹配=*>
< XSL:参数名称=parentElm>
< XSL:选择=名称(..)/&GT价值的;
< / XSL:参数>
< XSL:选择>
< XSL:当测试=本地名()='deliverydateasked'>
< XSL:元素的名称=deliverydateasked>
< XSL:呼叫模板名称=FormatDate>
< XSL:with-param中NAME =日期时间选择= />中。
< / XSL:呼叫模板>
< / XSL:组件>
< / XSL:当>
< XSL:否则>
< XSL:元素的名称={本地名()}>
< XSL:复制的选择=@ */>
< XSL:申请-模板选择=@ * |节点()/>
< / XSL:组件>
< / XSL:另有>
< / XSL:选择>
< / XSL:模板>

< XSL:模板名称=FormatDate>
< XSL:参数名称=日期时间/>
< XSL:变量名=约会>
< XSL:选择=串 - 前($日期时间,'T')/&GT价值的;
< / XSL:变量>

< XSL:如果测试=!字符串长度($日期)= 10>
< XSL:选择=$日期时间/&GT价值的;
< / XSL:如果>
< XSL:如果测试=字符串长度($日期)= 10>
< XSL:value-of的选择=$日/>
< / XSL:如果>
< / XSL:模板>
 

I'm trying to transform a datetime to a date format yyyy-MM-dd, because I'm using the xsd.exe tool the xs:date datatypes are automatically changed into a datetime datatype, because there is no type in the .NET Framework that matches the type xs:date completely.

But I can't get it to work

<articles>
        <article>
          <articleid>48992</articleid>
          <deliverydateasked>2009-01-29T00:00:00+01:00</deliverydateasked>
        </article>
        <article>
          <articleid>48993</articleid>
          <deliverydateasked>2009-01-30T00:00:00+01:00</deliverydateasked>
        </article>
</articles>

trying to convert the xml to

<articles>
        <article>
          <articleid>48992</articleid>
          <deliverydateasked>2009-01-29</deliverydateasked>
        </article>
        <article>
          <articleid>48993</articleid>
          <deliverydateasked>2009-01-30</deliverydateasked>
        </article>
</articles>

currently I'm using this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
	<articles>
		<xsl:apply-templates select="article">
		</xsl:apply-templates>
            </articles>
</xsl:template>

<xsl:template name="FormatDate">

	<xsl:param name="DateTime" />
	<xsl:variable name="date">
		<xsl:value-of select="substring-before($DateTime,'T')" />
	</xsl:variable>

	<xsl:if test="string-length($date) != 10">
		<xsl:value-of select="$DateTime"/>
	</xsl:if>
	<xsl:if test="string-length($date) = 10">
		<xsl:value-of select="$date"/>
	</xsl:if>
</xsl:template>

<xsl:template match="article">
		<xsl:call-template name="FormatDate">
			<xsl:with-param name="DateTime" select="deliverydateasked"/>
		</xsl:call-template>	
</xsl:template>

Does anyone know a good xslt transformation.

Thanks in advance

The output result of my code is

<articles />

解决方案

Thanks to Stesoc and annakata I figured it out This is the code I'm now using and it works perfect

<xsl:template match="*">
	<xsl:param name="parentElm">
		<xsl:value-of select="name(..)" />
	</xsl:param>
	<xsl:choose>
		<xsl:when test="local-name() = 'deliverydateasked'">
			<xsl:element name="deliverydateasked">
				<xsl:call-template name="FormatDate">
					<xsl:with-param name="DateTime" select="."/>
				</xsl:call-template>
			</xsl:element>
		</xsl:when>
		<xsl:otherwise>
			<xsl:element name="{local-name()}">
				<xsl:copy-of select="@*" />
				<xsl:apply-templates select="@* | node()" />
			</xsl:element>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="FormatDate">
	<xsl:param name="DateTime" />
	<xsl:variable name="date">
		<xsl:value-of select="substring-before($DateTime,'T')" />
	</xsl:variable>

	<xsl:if test="string-length($date) != 10">
		<xsl:value-of select="$DateTime"/>
	</xsl:if>
	<xsl:if test="string-length($date) = 10">
		<xsl:value-of select="$date"/>
	</xsl:if>
</xsl:template>