2

我正在努力解决一个问题 - 我试图在 XSLT 中插入当前日期时间,但由于语法不正确而出现错误。我的 XML 文件没有日期时间,因此我需要在 XSLT 文件中插入当前日期时间(带有属性的 date="") - 如下所示:

XSLT:

<TestList>
  <Header testCode="Test_3334"  testId="" date="">
    <xsl:attribute name="Header/date">
        <xsl:value-of  select="current-dateTime()"/>
      </xsl:attribute>
    <Validation TestName="{Header/Validation/TestName}" TestSurname="{Header/Validation/Surname}" checksum="{Header/Validation/Checksum}" />
  </Header>
  <Tests>
    <xsl:apply-templates select="Tests/Test"/>
  </Tests>
</TestList>

有没有办法在 XSLT 中格式化正确的日期时间。也许我的代码是错误的。谢谢你的帮助 :)

4

1 回答 1

5

The issue may not be with the "current-dateTime()" function, but with the name of the attribute:

 <xsl:attribute name="Header/date">

You should not specify an xpath expression here, but literally just the name of the attribute, and it will be added to the most recent element you have output

 <xsl:attribute name="date">

You also don't actually need to have the "date" attribute specified on the Header first either (although that won't break anything, as the xsl:attribute will overwrite it). This should work:

<Header testCode="Test_3334" testId="">
    <xsl:attribute name="date">
        <xsl:value-of  select="current-dateTime()"/>
    </xsl:attribute>

Actually, you can simplify this with Attriute Value Templates. Try this

<Header testCode="Test_3334" testId="" date="{current-dateTime()}">

Note that you will need to be using an XSLT 2.0 processor for the dateTime function to work.

于 2013-10-16T14:52:38.717 回答