2015-05-04 50 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> 
相關問題