如何使用XSLT组织图表

时间:2015-03-07 01:14:04

标签: xml xslt

我有一个XSLT文件,我需要组织一个图表来显示编程语言的功能。对于每一列,如果特定的编程语言行具有该功能,则需要打印" yes"。有任何想法吗? XLST仍然很新。我在想是否有陈述。

这是我到目前为止所拥有的。例如,C是必要的和程序性的,所以"是"需要在那些列行中。

到目前为止,这是我的代码

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >    

    <xsl:template match="/" >
    <html>
    <head>
    <title>Programming</title>
    </head>
    <body>
     <table border="1">
      <tr>
        <th style="text-align:left">Programming</th>
        <th style="text-align:left">Intended Use</th>
        <th style="text-align:left">Imperaitve</th>
        <th style="text-align:left">Object-Oriented</th>
        <th style="text-align:left">Functional</th>
        <th style="text-align:left">Procedural</th>
        <th style="text-align:left">Generic</th>
        <th style="text-align:left">Reflective</th>
        <th style="text-align:left">Event-Driven</th>
        <th style="text-align:left">Standards</th>
      </tr>
      <xsl:for-each select="programming/languages/language">
      <tr>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:variable name="intendedUseID" select="purpose/intendedUse/@id" />
        <xsl:for-each select="/programming/purpose/intendedUse[@id=$intendedUseID]" >
           <xsl:value-of select="." /> <br  />
        </xsl:for-each></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td>   <xsl:variable name="standardID" select="standards/standard/@id" />
        <xsl:for-each select="/programming/standards/standard[@id=$standardID]" >
           <xsl:value-of select="." /> <br />
        </xsl:for-each></td>
      </tr>
      </xsl:for-each>
    </table>

    </body>
    </html>

    </xsl:template>

</xsl:stylesheet>
      
    <programming>
      <languages>
        <language id="1">
          <name>C</name>
          <purpose>
            <intendedUse id="1" />
            <intendedUse id="2" />
            <intendedUse id="3" />
            <intendedUse id="4" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="4" />
          </features>
          <standards>
            <standard id="1" />
            <standard id="2" />
          </standards>
        </language>
        <language id="2">
          <name>C++</name>
          <purpose>
            <intendedUse id="1" />
            <intendedUse id="2" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="2" />
            <feature id="3" />
            <feature id="4" />
            <feature id="5" />
          </features>
          <standards>
            <standard id="2" />
          </standards>
        </language>
        <language id="3">
          <name>C#</name>
          <purpose>
            <intendedUse id="1" />
            <intendedUse id="5" />
            <intendedUse id="6" />
            <intendedUse id="7" />
            <intendedUse id="3" />
            <intendedUse id="8" />
            <intendedUse id="9" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="2" />
            <feature id="3" />
            <feature id="4" />
            <feature id="5" />
            <feature id="6" />
            <feature id="7" />
          </features>
          <standards>
            <standard id="3" />
            <standard id="2" />
          </standards>
        </language>
        <language id="4">
          <name>Java</name>
          <purpose>
            <intendedUse id="1" />
            <intendedUse id="6" />
            <intendedUse id="7" />
            <intendedUse id="3" />
            <intendedUse id="8" />
            <intendedUse id="9" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="2" />
            <feature id="5" />
            <feature id="6" />
          </features>
          <standards>
            <standard id="4" />
          </standards>
        </language>
        <language id="5">
          <name>JavaScript</name>
          <purpose>
            <intendedUse id="7" />
            <intendedUse id="8" />
            <intendedUse id="7" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="2" />
            <feature id="3" />
            <feature id="6" />
          </features>
          <standards>
            <standard id="3" />
          </standards>
        </language>
        <language id="6">
          <name>PHP</name>
          <purpose>
            <intendedUse id="8" />
            <intendedUse id="9" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="2" />
            <feature id="4" />
            <feature id="6" />
          </features>
          <standards>
            <standard id="5" />
          </standards>
        </language>
        <language id="7">
          <name>Python</name>
          <purpose>
            <intendedUse id="1" />
            <intendedUse id="3" />
            <intendedUse id="9" />
            <intendedUse id="10" />
            <intendedUse id="11" />
            <intendedUse id="12" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="2" />
            <feature id="3" />
            <feature id="6" />
          </features>
          <standards>
            <standard id="5" />
          </standards>
        </language>
        <language id="8">
          <name>Basic</name>
          <purpose>
            <intendedUse id="1" />
            <intendedUse id="13" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="4" />
          </features>
          <standards>
            <standard id="1" />
            <standard id="2" />
          </standards>
        </language>
        <language id="9">
          <name>ADA</name>
          <purpose>
            <intendedUse id="1" />
            <intendedUse id="2" />
            <intendedUse id="14" />
            <intendedUse id="15" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="2" />
            <feature id="4" />
            <feature id="5" />
          </features>
          <standards>
            <standard id="1" />
            <standard id="2" />
            <standard id="6" />
          </standards>
        </language>
        <language id="10">
          <name>ActionScript 3.0</name>
          <purpose>
            <intendedUse id="1" />
            <intendedUse id="7" />
            <intendedUse id="9" />
          </purpose>
          <features>
            <feature id="1" />
            <feature id="2" />
            <feature id="7" />
          </features>
          <standards>
            <standard id="3" />
          </standards>
        </language>
      </languages>

      <purpose>
        <intendedUse id="1"> Application</intendedUse>
        <intendedUse id="2"> System </intendedUse>
        <intendedUse id="3"> General Purpose </intendedUse>
        <intendedUse id="4"> Low-Level Operations </intendedUse>
        <intendedUse id="5"> RAD </intendedUse>
        <intendedUse id="6"> Business </intendedUse>
        <intendedUse id="7"> Client-Side </intendedUse>
        <intendedUse id="8"> Server-Side </intendedUse>
        <intendedUse id="9"> Web </intendedUse>
        <intendedUse id="10"> Scripting </intendedUse>
        <intendedUse id="11"> AI </intendedUse>
        <intendedUse id="12"> Scientific Computing </intendedUse>
        <intendedUse id="13">Education</intendedUse>
        <intendedUse id="14">Embedded</intendedUse>
        <intendedUse id="15">RealTime</intendedUse>
      </purpose>
      <features>
        <feature id="1">Imperative </feature>
        <feature id="2">Object-Oriented</feature>
        <feature id="3">Functional</feature>
        <feature id="4">Procedural</feature>
        <feature id="5">Generic</feature>
        <feature id="6">Reflective</feature>
        <feature id="7">Event-Driven</feature>
      </features>
      <standards>
        <standard id="1">ANSI</standard>
        <standard id="2">ISO</standard>
        <standard id="3">ECMA</standard>
        <standard id="4">DE FACTO</standard>
        <standard id="5">NO</standard>
        <standard id="6">GOST</standard>
      </standards>
    </programming>

1 个答案:

答案 0 :(得分:2)

我说这个问题更多的是关于制作交叉表报告而不是&#34; 组织图表&#34;。无论如何,我建议你这样看:

XSLT 1.0 (最小化到手头的问题)

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:variable name="all-features" select="/programming/features/feature" />

<xsl:template match="/">
    <table border="1">
        <tr>
            <th>Language</th>
            <xsl:for-each select="$all-features">
                <th><xsl:value-of select="."/></th>
            </xsl:for-each>
        </tr>
        <xsl:apply-templates select="programming/languages/language"/>
    </table>
</xsl:template>

<xsl:template match="language">
    <tr>
        <th><xsl:value-of select="name"/></th>
        <xsl:variable name="my-features" select="features/feature" />
        <xsl:for-each select="$all-features">
            <td>
                <xsl:if test="@id = $my-features/@id">Yes</xsl:if>
            </td>
        </xsl:for-each>
    </tr>
</xsl:template>

</xsl:stylesheet>

(渲染)结果,当应用于您的输入时:

enter image description here