我有一个XML文件,用于存储学校课程的数据.我刚刚开始乱用SVG,因此制作了一个SVG文件来表示每个班级的注册号码.这是我提出的缩小版本:
第一个栏代表在第一堂课注册的25名学生,依此类推.
既然我已经学习了一些基本的XSLT,我想看看我是否可以从下面发布的XML文件中提取这些注册号码,而不是仅手动输入数字(就像我创建上面的图像一样),因为那样太容易了那是我遇到麻烦的地方.我相信大部分信息是正确的,但是如果你看一下我下面的XSLT文件,你会看到我将每个矩形的高度设置为15,我想将它乘以注册号(所以第一个栏的高度应该是15 * 25,其中25个是通过XSLT从XML文件中提取的.第二个栏应该是15 * 20,因为第二个栏的注册是20,依此类推).我开始很好,我认为我很接近,但在我开始添加模板后,条形图消失了.任何帮助,将不胜感激!
当前的XSLT文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <!-- main template -->
- <xsl:template match="/">
- <!-- root element is svg and its namespace -->
- <svg version="1.0"
- xmlns="http://www.w3.org/2000/svg">
- <!-- Vertical red line -->
- <line x1="30" y1="35" x2="30" y2="500"
- style="stroke:rgb(255,0);stroke-width: 3" />
- <!-- Horizontal red line -->
- <line x1="30" y1="500" x2="500" y2="500"
- style="stroke:rgb(255,0);stroke-width: 3" />
- <!-- apply templates to display rectangle bars-->
- <xsl:apply-templates select="courses/course/enrollment" />
- </svg>
- </xsl:template>
- <!-- Rectangle 1 template -->
- <xsl:template match="enrollment[1]">
- <!-- Blue Rectangle 1 (341-01) -->
- <rect x="40" y="{500-@height}" width="30" height="{15*.}"
- style="fill:rgb(255,0);stroke-width: 3;stroke:rgb(0,0)" />
- </xsl:template>
- <!-- Rectangle 2 template -->
- <xsl:template match="enrollment[2]">
- <!-- Blue Rectangle 2 (341-02) -->
- <rect x="100" y="{500-@height}" width="30" height="{15*.}"
- style="fill:rgb(255,0)" />
- </xsl:template>
- <!-- Rectangle 3 template -->
- <xsl:template match="enrollment[3]">
- <!-- Blue Rectangle 3 (341-03) -->
- <rect x="160" y="{500-@height}" width="30" height="{15*.}"
- style="fill:rgb(255,0)" />
- </xsl:template>
- <!-- Rectangle 4 template -->
- <xsl:template match="enrollment[4]">
- <!-- Blue Rectangle 4 (368-01) -->
- <rect x="220" y="{500-@height}" width="30" height="{15*.}"
- style="fill:rgb(255,0)" />
- </xsl:template>
- <!-- Rectangle 4 template -->
- <xsl:template match="enrollment[4]">
- <!-- Blue Rectangle 5 (375-01) -->
- <rect x="280" y="{500-@height}" width="30" height="{15*.}"
- style="fill:rgb(255,0)" />
- </xsl:template>
- <!-- Rectangle 4 template -->
- <xsl:template match="enrollment[4]">
- <!-- Blue Rectangle 6 (385-01) -->
- <rect x="340" y="{500-@height}" width="30" height="{15*.}"
- style="fill:rgb(255,0)" />
- </xsl:template>
- <!-- Rectangle 5 template -->
- <xsl:template match="enrollment[5]">
- <!-- Blue Rectangle 7 (413-01) -->
- <rect x="400" y="{500-@height}" width="30" height="{15*.}"
- style="fill:rgb(255,0)" />
- </xsl:template>
- </xsl:stylesheet>
XML文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <courses>
- <course number="3221" credits="4.0">
- <title>Physics</title>
- <section number="01" delivery="Classroom">
- <enrollment>25</enrollment>
- <room>EA244</room>
- <instructor>
- <first>Herman</first>
- <last>Johnson</last>
- </instructor>
- </section>
- <section number="02" delivery="Online">
- <enrollment>20</enrollment>
- <instructor>
- <first>Herman</first>
- <last>Johnson</last>
- </instructor>
- <instructor>
- <first>Mike</first>
- <last>Miller</last>
- </instructor>
- </section>
- <section number="03" delivery="Classroom">
- <enrollment>12</enrollment>
- <room>SH102</room>
- <instructor>
- <first>Allison</first>
- <last>Sweeney</last>
- </instructor>
- </section>
- </course>
- <course number="1318" credits="4.0">
- <title>Psychology</title>
- <section number="01" delivery="Classroom">
- <enrollment>9</enrollment>
- <room>AT102</room>
- <instructor>
- <first>Mike</first>
- <last>Miller</last>
- </instructor>
- <instructor>
- <first>Alex</first>
- <last>Holmquist</last>
- </instructor>
- </section>
- </course>
- <course number="3715" credits="4.0">
- <title>Biology</title>
- <section number="01" delivery="ITV">
- <enrollment>18</enrollment>
- <room>EA244</room>
- <instructor>
- <first>Mike</first>
- <last>Miller</last>
- </instructor>
- </section>
- </course>
- <course number="3815" credits="3.0">
- <title>Calculus</title>
- <section number="01" delivery="Classroom">
- <enrollment>16</enrollment>
- <room>ST108</room>
- <instructor>
- <first>Herman</first>
- <last>Johnson</last>
- </instructor>
- </section>
- </course>
- <course number="4113" credits="3.0">
- <title>Chemistry</title>
- <section number="01" delivery="Online">
- <enrollment>20</enrollment>
- <instructor>
- <first>Mike</first>
- <last>Miller</last>
- </instructor>
- </section>
- </course>
- </courses>
编辑:
以下是在XSLT转换后SVG代码应该是什么样子:
- <?xml version="1.0" encoding="UTF-8"?>
- <svg xmlns="http://www.w3.org/2000/svg" version="1.0">
- <line style="stroke:rgb(255,0);stroke-width: 3" y2="500" x2="30" y1="35" x1="30" />
- <line style="stroke:rgb(255,0);stroke-width: 3" y2="500" x2="500" y1="500" x1="30" />
- <rect style="fill:rgb(255,0)" height="375" width="30" y="125" x="40" />
- <rect style="fill:rgb(255,0)" height="300" width="30" y="200" x="100" />
- <rect style="fill:rgb(255,0)" height="180" width="30" y="320" x="160" />
- <rect style="fill:rgb(255,0)" height="135" width="30" y="365" x="220" />
- <rect style="fill:rgb(255,0)" height="270" width="30" y="230" x="280" />
- <rect style="fill:rgb(255,0)" height="240" width="30" y="260" x="340" />
- <rect style="fill:rgb(255,0)" height="300" width="30" y="200" x="400" />
- </svg>
重申一下,每个矩形的高度应该来自任何登记号乘以15(否则矩形条将太短).我也希望它按顺序排列,因此第一个栏应该是第一个登记号码乘以15,最后一个栏位应该是最后一个登记号码乘以15.
让我建议以下为起点:
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns="http://www.w3.org/2000/svg">
- <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
- <xsl:strip-space elements="*"/>
- <xsl:template match="/">
- <svg version="1.0">
- <g transform="translate(0 500) scale(1 -1)">
- <xsl:apply-templates select="courses/course/section"/>
- </g>
- </svg>
- </xsl:template>
- <xsl:template match="section">
- <rect x="{40 * position()}" width="30" height="{enrollment}"/>
- </xsl:template>
- </xsl:stylesheet>
也可以看看:
Using XSLT and SVG to create bar chart from XML – Scaling Bar Chart