2015-05-04 40 views
2

我是很新的XSL和我得到了以下问题:XSLT - 与配对多次出现两个节点的内容到一个输出每次出现

有,有很多在这节点的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文件以显示结果(巡航控件插件)。问题是,我想配对'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'交替?

+0

你的问题不清楚:'sysinfo'和'vminfo'节点是成对的吗?或者他们在单独的区块?什么是你想要的输出?你的样式表说“html”,但是它输出文本(而不是你显示的输出)。请澄清。 –

+0

'sysinfo'和'vminfo'都是'cruisecontrol'的childelement。它们都有多个外观,并且总是相互依存(第一个'sysinfo'总是属于第一个'vminfo',第二个'sysinfo'属于第二个'vminfo'等等,但是它们可以被其他的childelement '巡航控制')。输出将是HMTL,但是我从代码中删除了HTML部分以使其更短。 – Sarkkhan

+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这应该引起交替。

+0

非常感谢您的帮助。它现在有效。我开始编写基于w3schoosl教程的XSL,但本教程错过了像“following-sibling”这样的存在: – Sarkkhan

+0

哦,它没有:http://www.w3schools.com/xpath/xpath_axes.asp – leu

0

给出的例子输入:

XML

<cruisecontrol> 
    <sysinfo> 
     <info>S1</info> 
    </sysinfo> 
    <vminfo> 
     <info>V1</info> 
    </vminfo> 
    <sysinfo> 
     <info>S2</info> 
    </sysinfo> 
    <vminfo> 
     <info>V2</info> 
    </vminfo> 
    <sysinfo> 
     <info>S3</info> 
    </sysinfo> 
    <vminfo> 
     <info>V3</info> 
    </vminfo> 
</cruisecontrol> 

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" indent="yes"/> 

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

<xsl:template match="sysinfo"> 
    <div> 
     <xsl:apply-templates select="info | following-sibling::vminfo[1]/info"/> 
    </div> 
</xsl:template> 

<xsl:template match="info"> 
    <p> 
     <xsl:value-of select="."/> 
    </p> 
</xsl:template> 

</xsl:stylesheet> 

将返回:

<div> 
    <div> 
     <p>S1</p> 
     <p>V1</p> 
    </div> 
    <div> 
     <p>S2</p> 
     <p>V2</p> 
    </div> 
    <div> 
     <p>S3</p> 
     <p>V3</p> 
    </div> 
</div> 
相关问题