我正在从XML格式转换到另一个格式(使用更新的架构)。我用 altova mapforce 做了XSLT,我正在用C#编写一个程序来转换这些文件。
这里的问题是日期格式,XSLT正在执行此作业,但C#给出了错误的输出(20-14-xx-xx)。
到目前为止,这是我的C#代码:
private void pictureBox3_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "XML Files|*.xml";
if (open.ShowDialog() == DialogResult.OK)
{
try
{
XDocument xmlDocument = XDocument.Load(open.FileName);
XDocument transformedDoc = new XDocument();
using (XmlWriter writer = transformedDoc.CreateWriter())
{
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(XmlReader.Create(new StreamReader(@"C:\XSLT\Converter.xslt")));
transform.Transform(xmlDocument.CreateReader(), writer);
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XML Files|*.xml";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
transformedDoc.Save(saveFileDialog.FileName);
}
{
MessageBox.Show("Transform complete");
}
}
catch
{
MessageBox.Show("Error");
}
}
}
这是XSLT中转换日期的行:
<IssueDate>
<xsl:value-of select="translate(format-number(number(string($var8_resultof_first/IssueDate)), '####,##,##'), '.,', concat($var13_shared, $var1_resultof_first))"/>
</IssueDate>
可以在C#代码中更正此节点吗?
答案 0 :(得分:1)
格式化日期:
<IssueDate>20141031</IssueDate>
为:
<IssueDate>2014-10-31</IssueDate>
使用:
<xsl:value-of select="concat(substring(IssueDate, 1, 4), '-', substring(IssueDate, 5, 2), '-', substring(IssueDate, 7, 2))" />
如果您需要使用多个日期执行此操作,请使用命名模板:
<xsl:template name="reformat-date">
<xsl:param name="yyyymmdd"/>
<xsl:value-of select="concat(substring($yyyymmdd, 1, 4), '-', substring($yyyymmdd, 5, 2), '-', substring($yyyymmdd, 7, 2))" />
</xsl:template>
电话示例:
<xsl:call-template name="reformat-date">
<xsl:with-param name="yyyymmdd" select="IssueDate"/>
</xsl:call-template>
注意:您无法使用format-number()
创建不等大小的数字组。
答案 1 :(得分:0)
<IssueDate>
<xsl:value-of select="concat(concat(concat(concat(substring($var6_resultof_string, number('1'), number('4')), '-'), substring($var6_resultof_string, number('5'), $var11_resultof_cast)), '-'), substring($var6_resultof_string, number('7'), $var11_resultof_cast))"/>
</IssueDate>
最后,在michael.hor257k的帮助下,我做了正确的修改以获得正确的输出,谢谢m8。 :)