我目前正在尝试访问来自多个 XML 文件的数据。我很容易从第一个名为 Rainfall.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 工作表样式的相应 Rainfall.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
无论如何只包含一个元素)。
请注意,由于我不得不猜测您的实际 输入文档结构,因此可能会关闭一些 XPath。相应地调整它们。
关于xml - 通过 XSL 访问多个 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32775600/