2017-09-04 78 views
0

我有一個XML,我需要從所有節點中選擇特定的元素。實際的XML的結構看起來像這 -xslt驗證一個節點的元素屬性或不

<host> 
<node> 
    <type>fruit1</type> 
    <value>10</value> 
</node> 
<node> 
    <type>fruit2</type> 
    <value>20</value> 
</node> 
<node> 
    <type>fruit3</type> 
    <value xsi:type="valueList"> 
     <listValues> 
     <value> 
      <value>30</value> 
      <code>abc</code> 
     </value> 
     </listValues> 
    </value> 
</node> 
<node> 
    <type>fruit4</type> 
    <value>40</value> 
</node> 
<node> 
    <type>fruit5</type> 
    <value>50</value> 
</node> 
</host> 

我不得不選擇從所有節點以表格格式元素<type><value>。第三個節點具有元素<value>的列表值,對於此元素,值應爲「true」。預期輸出XML是如下 -

<html> 
<body> 
<table border="1"> 
<tr> 
    <td>fruit1</td> 
    <td>10</td> 
</tr> 
<tr> 
    <td>fruit2</td> 
    <td>20</td> 
</tr> 
<tr> 
    <td>fruit3</td> 
    <td>true</td> 
</tr> 
<tr> 
    <td>fruit4</td> 
    <td>40</td> 
</tr> 
<tr> 
    <td>fruit5</td> 
    <td>50</td> 
</tr> 
</table> 
</body> 
</html> 

編寫的XSLT如下 -

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
<body> 
<table border="1"> 
    <xsl:for-each select="host/node"> 
    <tr> 
     <td><xsl:value-of select="type" /></td> 
     <xsl:choose> 
     <xsl:when test = "//node/value/listValues/value/value != null"> 
      <td>true</td> 
     </xsl:when> 
     <xsl:otherwise> 
      <td><xsl:value-of select="value" /></td> 
     </xsl:otherwise> 
     </xsl:choose> 
    </tr> 
    </xsl:for-each> 
</table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

但我無法得到想要的XML輸出格式。任何幫助將是一個很大的優點。

回答

1

變化

<xsl:when test = "//node/value/listValues/value/value != null"> 

<xsl:when test = "value/listValues/value/value != ''"> 
+0

Rupesh嗨,這是現在的工作謝謝。當給定xml中有多個節點(如節點3)時,是否有方法可以爲編寫相對路徑的腳本? –

+0

您可以使用作爲相對路徑 – Rupesh

相關問題