2014-09-19 86 views
1

嗨, 我想從xml中使用xslt提取屬性值。片段在下面。XSLT:獲取屬性值嵌套時

XSlt從屬性中提取數據。在循環提取屬性數據時使用。

<?xml version="1.0" encoding="iso-8859-1"?> 
<!-- Edited by XMLSpy® --> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <html> 
     <body> 
     <h2>My CD Collection</h2> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
      <th>Title</th> 
      <th>Artist</th> 
      </tr> 
      <tr> 
      <xsl:choose> 
       <xsl:when test="//catalog/cd/title/artist/country/item/@id='mod1'"> 
       <td>asdfg</td> 
       <xsl:choose> 
        <xsl:when test="//catalog/cd/title/artist/country/item/item/@id='up1'"> 
        <td> 
         <xsl:value-of select="//catalog/cd/title/artist/country/item/item/@value" /> 
        </td> 
        </xsl:when> 
        <xsl:otherwise/> 
       </xsl:choose> 
       </xsl:when> 
       <xsl:otherwise/> 
      </xsl:choose> 
      </tr> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

要解析的XML數據。

<?xml version="1.0" encoding="utf-8"?> 
<!-- Edited by XMLSpy --> 
<catalog> 
    <cd> 
    <title> 
     <artist> 
     <country> 

      <item type="layer" id="mod" title="Mod" result="pass"> 
      <item type="measure" id="up" title="Up" value="10" unit="dBm" /> 
      <item type="measure" id="down" title="Down" value="9.6" unit="dBm" /> 

      </item> 

      <item type="layer" id="mod1" title="Mod1" result="pass"> 
      <item type="measure" id="up1" title="Up" value="100" unit="dBm" /> 
      <item type="measure" id="down1" title="Down" value="9.60" unit="dBm" /> 

      </item> 

     </country> 
     </artist> 
    </title> 
    </cd> 

</catalog> 

的預期輸出和實際輸出下面:

Expected output: 

My CD Collection 

Title | Artist 
asdfg | 100 




Actual Output: 

My CD Collection 

Title | Artist 
asdfg | 10 

將是一個解決方案真的很感謝。提前致謝。

回答

0

基本上,你的XSLT的邏輯是說:

  • 如果任何項ID MOD1
    • 然後顯示值爲 「asdfg」
    • 如果任何表格單元格商品商品ID已上架1
    • 然後顯示文件中第一件商品的value屬性

要完成你正在嘗試做的,你可以這樣做:

<?xml version="1.0" encoding="iso-8859-1"?> 
<!-- Edited by XMLSpy® --> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <html> 
     <body> 
     <h2>My CD Collection</h2> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
      <th>Title</th> 
      <th>Artist</th> 
      </tr> 
      <tr> 
      <xsl:variable name="mod1" 
          select="//catalog/cd/title/artist/country/item[@id = 'mod1']" /> 
      <xsl:if test="$mod1"> 
       <td>asdfg</td> 
       <xsl:variable name="up1" select="$mod1/item[@id = 'up1']" /> 
       <xsl:if test="$up1"> 
       <td> 
        <xsl:value-of select="$up1/@value" /> 
       </td> 
       </xsl:if> 
      </xsl:if> 
      </tr> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

作爲XSLT去,這是不是寫得好,什麼XSLT看起來很有代表性,但我我把它作爲一個例子,因爲我認爲這不是你的最終目標,你試圖把它作爲你邁向最終目標的一步。

對於下一步,我建議開一個新的問題。