我目前正在尝试访问来自多个XML文件的数据。我很容易从第一个称为rain .xml的文件中访问数据,但无法从我的列表中的下一个文件Max_temp.xml中检索任何数据。
总体目标是将4-5个XML文件组合在一起,以包含有关各种天气事件的所有数据以及记录这些事件的气象站。
示例代码如下:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<!-- TODO customize transformation rules
syntax recommendation http://www.w3.org/TR/xslt
-->
<xsl:variable name="maxTemp" select="document('Max_temp.xml')" />
<xsl:template match="rainfall">
<weather>
<xsl:apply-templates select="measurement" />
</weather>
</xsl:template>
<xsl:template match="measurement">
<measurement>
<StationNum><xsl:value-of select="StationNum"/></StationNum>
<Date><xsl:value-of select="concat(Day,'/',Month,'/',Year)"/></Date>
<xsl:variable name="Date" select="concat(Day,'/',Month'/',Year)"/>
<Rainfall><xsl:value-of select="Volume"/></Rainfall>
<MaxTemp><xsl:value-of select="$MaxTemp/maxTemp/measurement[concat(Day,'/',Month'/',Year)].equals(Date)"/></MaxTemp>
</measurement>
</xsl:template>
</xsl:stylesheet>
使用的XML文件结构如下:
<typeOfFile(Rainfall, Temp, Solar Radiation etc)>
<measurment>
<Code>...</Code>
<Station>...</Station>
<Day>...</Day>
<Month>...</Month>
<Year>...</Year>
<Volume>...</Volume>
</measurement>
</typeOfFile>
当尝试加载相应的XSL表样式的rain .xml文件时,我目前从浏览器获得无响应。
谁能给我指个正确的方向?另外,如果有人能给我提供一些关于使用XSL表创建和格式化XML文件的信息,我将不胜感激。可以使用以下方法(XSLT 1.0):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<!-- select the <measurement> elements of all the various input files -->
<xsl:variable name="maxTemp" select="document('Max_temp.xml')/*" />
<xsl:variable name="rainfall" select="document('rainfall.xml')/*" />
<xsl:variable name="solarRadiation" select="document('solar_radiation.xml')/*" />
<!-- index <measurement> elements by their station and date -->
<xsl:key name="kMeasurement" match="measurement"
use="concat(Station, '/', Day, '/', Month, '/', Year)"
/>
<xsl:template match="/">
<weather>
<xsl:apply-templates select="$maxTemp/measurement" />
</weather>
</xsl:template>
<xsl:template match="measurement">
<xsl:variable name="currentKey" select="concat(Station, '/', Day, '/', Month, '/', Year)" />
<measurement>
<StationNum><xsl:value-of select="Station"/></StationNum>
<Date><xsl:value-of select="concat(Day, '/', Month, '/', Year)"/></Date>
<!-- since we are handling maxTemp measurements here, we can output that directly -->
<MaxTemp><xsl:value-of select="Value"/></MaxTemp>
<!-- to access the others we need a context switch and a key lookup -->
<xsl:for-each select="$rainfall">
<Rainfall><xsl:value-of select="key('kMeasurement', $currentKey)/Volume"/></Rainfall>
</xsl:for-each>
<xsl:for-each select="$solarRadiation">
<SolarRadiation><xsl:value-of select="key('kMeasurement', $currentKey)/Watt"/></SolarRadiation>
</xsl:for-each>
<!-- and so on -->
</measurement>
</xsl:template>
</xsl:stylesheet>
您可以将它应用于一个空的虚拟输入文档(类似于简单的<xml />
)。
然后将所有实际文档加载到变量中,并跟踪其中一个变量中的条目来创建输出。我选择了最大温度测量值,但如果所有文件都包含相同日期的数据点,那么哪个文件都无关紧要。
每个最大温度数据点产生一个输出<measurement>
元素。
为此,它使用<xsl:key>
从相关文档中提取正确的测量值。<xsl:key>
是一个键/值存储(即字典,哈希表):它通过某个键字符串索引节点,在我们的例子中是站点ID和日期的组合。
但是它只返回与当前节点在同一文档中的节点。因此,要输出Max_temp.xml
之外的任何内容,我们必须将上下文切换到另一个文档。<xsl:for-each>
可以做到这一点,所以我们在这里使用它来设置调用key()
的作用域($rainfall
和$solarRadiation
只保存一个元素)。