XSLT - 将具有多个外观的两个节点的内容配对到每个外观的一个输出中

时间:2015-05-04 14:21:37

标签: xml xslt

我对XSL很新,我遇到了以下问题:

有一个包含大量节点的XML文件。

<cruisecontrol>
 ...
 <sysinfo>
  <info/>
 </sysinfo>
 ...
 <vminfo>
  <info/>
 </vminfo>
 ...
 <sysinfo>
  <info/>
 </sysinfo>
 ...
 <vminfo>
  <info/>
 </vminfo>
 ...
 <sysinfo>
  <info/>
 </sysinfo>
 ...
 <vminfo>
  <info/>
 </vminfo>
 ...
</cruisecontrol>

'sysinfo'和'vminfo'节点不止一次出现,但在此XML文件中的节点数量相同。现在我需要将此文件转换为HTML文件以显示结果(cruisecontrol插件)。问题是,我想配对'sysinfo'和'vminfo'的内容。

我得到的只是......像这样:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>    

 <xsl:template match="/">
  <h1>System Information</h1>
  <xsl:apply-templates select="//sysinfo"/>
  <h1>VM Information</h1>
  <xsl:apply-templates select="//vminfo"/>
 </xsl:template>

 <xsl:template match="sysinfo">
   <xsl:apply-templates select="info"/>
 </xsl:template>

 <xsl:template match="vminfo">
   <xsl:apply-templates select="info"/>
 </xsl:template>

 <xsl:template match="info">
  <xsl:value-of select ="name(.)"/>
  <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

输出如下:

System Information
<sysinfo1>
 <info>
<sysinfo2>
 <info>

VM Information
<vminfo1>
 <info>
<vminfo2>
 <info>

但我想要的是:

<sysinfo1>
 <info>
<vminfo1>
 <info>

<sysinfo2>
 <info>
<vminfo2>
 <info>

是否有可能从'sysinfo'和'vminfo'交替获取'info'?

1 个答案:

答案 0 :(得分:0)

您的输入和输出都不是很清楚。但初看起来,我认为像下面那样更改XSLT可以让我们知道可以在这里做些什么:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>    

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

 <xsl:template match="sysinfo">
  <h1>System Information</h1>
   <xsl:apply-templates select="info"/>
  <h1>VM Information</h1>
  <xsl:apply-templates select="following-sibling::vminfo[1]"/>
 </xsl:template>

 <xsl:template match="vminfo">
   <xsl:apply-templates select="info"/>
 </xsl:template>

 <xsl:template match="info">
  <xsl:value-of select ="name(.)"/>
  <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

sysinfo内,您调用以下vminfo,这将导致更改。