在输出中添加根节点



我的输出看起来像

<TransactionLog TID="1400" SeqNo="3337446" SQLTransaction="Insert into TankerLoads Values(141221,53,299,18,1,426148,6,'Nov 19 2007 12:00AM','Dec 30 1899 12:59PM',3.00,20682,0,'Zevo','Nov 19 2007 12:00AM',0)" />

我需要添加<root>节点的位置,以便它看起来像下面这样

<root>
  <TransactionLog TID="1400" SeqNo="3337446" SQLTransaction="Insert into TankerLoads Values(141221,53,299,18,1,426148,6,'Nov 19 2007 12:00AM','Dec 30 1899 12:59PM',3.00,20682,0,'Zevo','Nov 19 2007 12:00AM',0)" />
</root>

我使用以下代码组合了所有记录,现在我需要添加根节点,我需要将其与输出方法作为文本进行区分。请帮助我。

<xsl:template match="text()">
  <xsl:value-of select="normalize-space(.)" />
</xsl:template>

您可以在变量中捕获转换的输出,并将类似于以下内容的转换应用于变量的内容

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:variable name="vResult1">
   <TransactionLog TID="1400"
     SeqNo="3337446"
     SQLTransaction="Insert into TankerLoads Values(141221,53,299,18,1,426148,6,'Nov 19 2007 12:00AM','Dec 30 1899 12:59PM',3.00,20682,0,'Zevo','Nov 19 2007 12:00AM',0)" />
</xsl:variable>
 <xsl:template match="node()|@*" mode="pass2">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*" mode="pass2"/>
  </xsl:copy>
 </xsl:template>
 <xsl:template match="/">
  <root>
   <xsl:apply-templates select="$vResult1/*" mode="pass2"/>
  </root>
 </xsl:template>
</xsl:stylesheet>

在任何 XML 文档(未使用)上执行此转换时,将生成所需的正确结果

<root>
   <TransactionLog TID="1400" SeqNo="3337446"
                   SQLTransaction="Insert into TankerLoads Values(141221,53,299,18,1,426148,6,'Nov 19 2007 12:00AM','Dec 30 1899 12:59PM',3.00,20682,0,'Zevo','Nov 19 2007 12:00AM',0)"/>
</root>

或者,更好的是,将现有转换修改为如下所示的内容:

 <xsl:template match="/">
  <root>
   <xsl:apply-templates/>
  </root>
 </xsl:template>

或者,如果您的转换已有与/匹配的模板:

 <xsl:template match="/">
  <root>
   <!-- Put the body of your current template here -->
  </root>
 </xsl:template>
</xsl:stylesheet>

最新更新