2015-12-02 26 views
0

我正在使用JavaFX應用程序,並且我有一個xml文件,我想將其轉換爲html。 xml文件具有一些具有相同屬性名稱但子名稱不同的元素。我想根據具有相同屬性值的父元素對子元素進行分組。xsl樣式表:獲取具有相同屬性值的父元素的子元素

的xml文件看起來是這樣的:

<?xml version="1.0" encoding="UTF-8" ?> 
 
<application> 
 
    <workflow name="Model Structure"> 
 
    <name>Model Structure</name> 
 
    <source>Model data</source> 
 
    <type>Aleatory</type> 
 
    <condition>independet</condition> 
 
    <distribution>None</distribution> 
 
    <assumptions>onsdn</assumptions> 
 
    </workflow> 
 
    <workflow name="Model Structure"> 
 
    <name>Model Structure</name> 
 
    <source>Poor data</source> 
 
    <type>Aleatory</type> 
 
    <condition>independet</condition> 
 
    <distribution>GLC</distribution> 
 
    <assumptions>nsisodsd</assumptions> 
 
    </workflow> 
 
    </application

我所要的輸出是這樣的:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
 
<html> 
 

 
<body> 
 

 
    <h3>Model Structure</h3> 
 
    <ol type="1"> 
 
    <li> 
 
     <p>Source: Model data</p> 
 
     <p>Type: Aleatory</p> 
 
     <p>Conditions: Independent</p> 
 
     <p>Distribution: GLV</p> 
 
     <p>Assumptions: sibsdisbdibs</p> 
 
    </li> 
 
    <li> 
 
     <p>Source: Poor data</p> 
 
     <p>Type: Epistemic with probabilities</p> 
 
     <p>Conditions: Independent</p> 
 
     <p>Distribution: GLV</p> 
 
     <p>Assumptions: sibsdisbdibs</p> 
 
    </li> 
 
    </ol> 
 

 
    <h3>Boundary Conditions</h3> 
 
    <ol type="1"> 
 
    <li> 
 
     <p>Source: Parametric conditions</p> 
 
     <p>Type: Aleatory</p> 
 
     <p>Conditions: Independent</p> 
 
     <p>Distribution: None</p> 
 
     <p>Assumptions: blablabla</p> 
 
    </li> 
 
    <li> 
 
     <p>Source: Some boundaries</p> 
 
     <p>Type: Epistemic with probabilities</p> 
 
     <p>Conditions: Independent</p> 
 
     <p>Distribution: GLV</p> 
 
     <p>Assumptions: sibsdisbdibs</p> 
 
    </li> 
 
    </ol> 
 

 
</body> 
 

 
</html>

我我在xsl樣式表中爲每個語句使用了一個,但是我找不到一個很好的方法來檢查迭代中下一個元素的值,並避免將它打印在html文件中。

<xsl:value-of select="@*" /> 
 
<xsl:for-each select="application/workflow"> 
 
    <xsl:variable name="tempname"> 
 
    <xsl:value-of select="name" /></xsl:variable> 
 
    <h3> <xsl:value-of select="@name"/> </h3> 
 
    <ol type="1"> 
 
    <ul> 
 
     <p>Source: 
 
     <xsl:value-of select="source" /> 
 
     </p> 
 
     <p>Type: 
 
     <xsl:value-of select="type" /> 
 
     </p> 
 
     <p>Conditions: 
 
     <xsl:value-of select="condition" /> 
 
     </p> 
 
     <p>Distribution: 
 
     <xsl:value-of select="distribution" /> 
 
     </p> 
 
     <p>Assumptions: 
 
     <xsl:value-of select="assumptions" /> 
 
     </p> 
 
    </ul> 
 
    </ol> 
 
</xsl:for-each>

+1

你可以使用XSLT 2.0? - P.S.有多個組的例子會很有用。 –

+0

我編輯了我想要顯示兩個組的結果。我嘗試使用元素與臨時變量,但xsl不允許更改變量的值。 –

+0

您是否理解使用XSLT 2.0的問題? –

回答

2

這裏有2個選項。第一種選擇將與1.0和2.0處理器兼容。第二個選項只適用於2.0處理器。

XSLT 1.0/2.0(實施例:http://xsltransform.net/pPzifpw

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/> 
    <xsl:strip-space elements="*"/> 

    <!--Group workflows by name attribute.--> 
    <xsl:key name="workflows" match="workflow" use="@name"/> 

    <xsl:template match="/*"> 
    <html> 
     <body> 
     <!--First workflow in the group.--> 
     <xsl:for-each select="workflow[generate-id()=generate-id(key('workflows',@name))]"> 
      <h3><xsl:value-of select="@name"/></h3> 
      <ol type="1"> 
      <!--All of the workflows in the group.--> 
      <xsl:apply-templates select="key('workflows',@name)"/> 
      </ol> 
     </xsl:for-each> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="workflow"> 
    <li> 
     <xsl:apply-templates select="*[not(self::name)]"/> 
    </li> 
    </xsl:template> 

    <xsl:template match="workflow/*"> 
    <!--Get the first character of the element name and make it uppercase.--> 
    <xsl:variable name="ic" 
     select="translate(substring(name(),1,1), 
     'abcdefghijklmnopqrstuvwxyz', 
     'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/> 
    <p><xsl:value-of select="concat($ic,substring(name(),2),': ',.)"/></p> 
    </xsl:template> 

</xsl:stylesheet> 

XSLT 2.0(實施例:http://xsltransform.net/bnnZWe

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/*"> 
    <html> 
     <body> 
     <!--First workflow in the group.--> 
     <xsl:for-each-group select="workflow" group-by="@name"> 
      <h3><xsl:value-of select="current-grouping-key()"/></h3> 
      <ol type="1"> 
      <!--All of the workflows in the group.--> 
      <xsl:apply-templates select="current-group()"/> 
      </ol> 
     </xsl:for-each-group> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="workflow"> 
    <li> 
     <xsl:apply-templates select="* except name"/> 
    </li> 
    </xsl:template> 

    <xsl:template match="workflow/*"> 
    <!--Get the first character of the element name and make it uppercase.--> 
    <xsl:variable name="name" 
     select="replace(name(),'^.',upper-case(substring(name(),1,1)))"/> 
    <p><xsl:value-of select="concat($name,': ',.)"/></p> 
    </xsl:template> 


</xsl:stylesheet> 
+0

非常了不起!我花了一些時間來嘗試瞭解過程。非常感謝您的分享!! –

相關問題