通过XSLT将输入的XML复制到输出

3
以下是我的输入XML。
<ServiceIncident xmlns="http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2">
    <RequesterID/>
    <ProviderID>INC0011731</ProviderID>
    <ProviderPriority>4</ProviderPriority>
    <WorkflowStatus>NEW</WorkflowStatus>
    <Transaction>
        <Acknowledge>1</Acknowledge>
        <StatusCode>0</StatusCode>
        <Comment>String</Comment>
        <TransactionName>Problem_Submittal</TransactionName>
        <TransactionType>2</TransactionType>
        <TransactionDateTime>2012-10-19T16:05:56Z</TransactionDateTime>
        <TransactionNumber>2012-10-19T16:05:56Z:1ae9b6a79901fc40fa75c64e1cdcc3b4</TransactionNumber>
        <TransactionRouting>MX::ITELLASNINCBRDG</TransactionRouting>
        <DataSource>ServiceNow</DataSource>
        <DataTarget>NASPGR72</DataTarget>
    </Transaction>
</ServiceIncident>

我的要求是需要将整个输入复制到输出,除了输入中的一个字段需要在输出中进行更改。

以下是我在xslt中使用的代码来复制输入:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:r2="http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2">
    <xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>           
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

在使用上述xslt代码将整个输入复制为输出时,但我的要求是需要映射TransactionDateTime而不是硬编码值。
      <TransactionDateTime>2012-10-19T16:05:56Z</TransactionDateTime>

我需要在事务中使用这个函数,而不是硬编码。 以下是我的XSLT代码,但它没有输出。

    <xsl:template match="r2:TransactionDateTime">
            <xsl:value-of select="current-dateTime()"/>
     </xsl:template>

1
可能是XSLT / X-PATH表达式的重复问题。 - Mathias Müller
1
current-dateTime()函数在XSLT 1.0中不可用 - 在该版本的XSLT中没有标准方法来获取当前时间,因此答案取决于您使用的XSLT处理器(您将需要一个特定于处理器的扩展函数)。 - Ian Roberts
...或者你需要更换一个能够理解XSLT 2.0的处理器。 - Ian Roberts
我尝试了XSLT 2.0,但它也不起作用。 - user3085529
它确实可以与XSLT2.0一起使用。您能告诉我们您正在使用哪个处理器吗? - Lingamurthy CS
欢迎来到Stackoverflow!如果您发现某个答案对您有用或最适合您的问题,请通过点击勾选标记(位于左侧箭头下方)接受该答案。 - Joel M. Lamsen
1个回答

5

添加另一个与要更改的节点匹配的模板,并在其中执行更改:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:r2="http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>           
    </xsl:copy>
</xsl:template>

<xsl:template match="r2:DataSource">
    <xsl:copy>Maximo</xsl:copy>
</xsl:template>
</xsl:stylesheet>

<xsl:template match="r2:TransactionDateTime"> <xsl:value-of select="r2:current-dateTime()"/> </xsl:template>。我需要在输出中获取此值。 - user3085529
如果第一个问题得到解决,您可以接受答案吗?对于新的需求,您可以发布一个新的问题。 - Lingamurthy CS
第一个是工作的,但还需要在输出中更改一个字段,即TransactionDateTime。我对它进行了相同的应用,但它不起作用。 - user3085529
好的,对于current-dateTime()函数,您不需要添加前缀"r2"。此外,在TransactionDateTime模板中,您缺少xsl:copy标签: <xsl:template match="r2:TransactionDateTime"> xsl:copy <xsl:value-of select="current-dateTime()"/> </xsl:copy> </xsl:template> - Lingamurthy CS
我尝试了这段代码,但它没有起作用,显示错误为current-date time with arity<1> is not in the in scope function declarations。 - user3085529
你可能应该使用XSLT2.0才能支持current-dateTime()。 - Lingamurthy CS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接