2014-09-19 88 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看起来很有代表性,但我我把它作为一个例子,因为我认为这不是你的最终目标,你试图把它作为你迈向最终目标的一步。

对于下一步,我建议开一个新的问题。