- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我目前正在尝试访问来自多个 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/
正如标题中所问,我有两个如下结构的 XML 文件 A.xml //here I want to include B.xml
我有一个 xml 文件。根据我的要求,我需要更新空标签,例如我需要更改 to .是否可以像那样更改标签.. 谢谢... 最佳答案 var xmlString=" "; var properStri
我有这样简单的 XML: Song Playing 09:41:18 Frederic Delius Violin Son
在我的工作中,我们有自己的 XML 类来构建 DOM,但我不确定应该如何处理连续的空格? 例如 Hello World 当它被读入 DOM 时,文本节点应该包含 Hello 和 World
我有以下 2 个 xml 文件,我必须通过比较 wd:Task_Name_ID 和 TaskID 的 XML 文件 2。 例如,Main XML File-1 wd:Task_Name_ID 具有以下
我在 Rails 应用程序中有一个 XML View ,需要从另一个文件插入 XML 以进行测试。 我想说“构建器,只需盲目地填充这个字符串,因为它已经是 xml”,但我在文档中看不到这样做的任何内容
我正在重建一些 XML 提要,因此我正在研究何时使用元素以及何时使用带有 XML 的属性。 一些网站说“数据在元素中,元数据在属性中。” 那么,两者有什么区别呢? 让我们以 W3Schools 为例:
在同一个文档中有两个 XML 声明是否是格式正确的 XML? hello 我相信不是,但是我找不到支持我的消息来源。 来自 Extensible Markup Language
我需要在包装器 XML 文档中嵌入任意(语法上有效的)XML 文档。嵌入式文档被视为纯文本,在解析包装文档时不需要可解析。 我知道“CDATA trick”,但如果内部 XML 文档本身包含 CDAT
XML 解析器和 XML 处理器是两个不同的东西吗?他们是两个不同的工作吗? 最佳答案 XML 解析器和 XML 处理器是一样的。它不适用于其他语言。 XML 是通用数据标记语言。解析 XML 文件已
我使用这个 perl 代码从一个文件中读取 XML,然后写入另一个文件(我的完整脚本有添加属性的代码): #!usr/bin/perl -w use strict; use XML::DOM; use
我正在编写一个我了解有限的历史脚本。 对象 A 的类型为 system.xml.xmlelement,我需要将其转换为类型 system.xml.xmldocument 以与对象 B 进行比较(类型
我有以下两个 XML 文件: 文件1 101 102 103 501 502 503
我有以下两个 XML 文件: 文件1 101 102 103 501 502 503
我有一个案例,其中一个 xml 作为输入,另一个 xml 作为输出:我可以选择使用 XSL 和通过 JAXB 进行 Unmarshalling 编码。性能方面,有什么真正的区别吗? 最佳答案 首先,程
我有包含 XML 的 XML,我想使用 JAXB 解析它 qwqweqwezxcasdasd eee 解析器 public static NotificationRequest parse(Strin
xml: mario de2f15d014d40b93578d255e6221fd60 Mario F 23 maria maria
尝试更新 xml 文件数组时出现以下错误。 代码片段: File dir = new File("c:\\XML"); File[] files = dir.listFiles(new Filenam
我怎样才能完成这样的事情: PS /home/nicholas/powershell> PS /home/nicholas/powershell> $date=(Get-Date | ConvertT
我在从 xml 文件中删除节点时遇到一些困难。我发现很多其他人通过各种方式在 powershell 中执行此操作的示例,下面的代码似乎与我见过的许多其他示例相同,但我没有得到所需的行为。 我的目标是将
我是一名优秀的程序员,十分优秀!