【发布时间】:2020-05-20 21:03:52
【问题描述】:
我有以下情况:在结构几乎相似的多个xml文件中,我需要提取一个属性和它来自的文件。
我相信我已经接近解决方案,但无法使其发挥作用。我是 xml 和 xslt 的新手,需要一些指导。谢谢。
我的多个xml文件都在同一个位置,它们的结构如下:
<erpConnector>
<sections>
<section name="Section1">
<!-- other irrelevant content here -->
</section>
<section name="Section2">
<!-- other irrelevant content here -->
</section>
<section name="Section3">
<!-- other irrelevant content here -->
</section>
</sections>
</erpConnector>
我创建了一个名为“report.xml”的单独 xml 文件,它就像我所有其他 xml 文件的聚合器一样。它看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="reports3.xsl" version="1.0"?>
<report>
<title>Section names</title>
<configfile name="C:\Users\.myFirstXMLfile.xml"/>
<configfile name="C:\Users\mySecondXMLfile.xml"/>
</report>
最后,我有 xsl 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="/report/title"/></title>
</head>
<body>
<xsl:for-each select="/report/configfile">
<xsl:apply-templates select="document(@name)/erpConnector"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template match="/">
<h1>
<xsl:value-of select="erpConnector/sections/section/@name"/>
<xsl:text> </xsl:text>
</h1>
</xsl:template>
</xsl:stylesheet>
我的输出只是 "
" 。
请告诉我可能是什么原因以及如何解决它。
输出可能如下所示:
<!DOCTYPE html>
<html>
<body>
<h2>Basic HTML Table</h2>
<table style="width:100%">
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<tr>
<td>Section1</td>
<td>myfirstxml</td>
</tr>
<tr>
<td>Section2</td>
<td>mysecondxml</td>
</tr>
</table>
</body>
</html>
谢谢!
【问题讨论】:
-
您有两个具有相同匹配模式的模板。只有最后一个正在应用。由于您的第一个模板将模板应用于
erpConnector,因此您的其他模板应该匹配。 -- 请注意,当前编写的模板试图输出文档中第 1 部分的名称 - 这不是您所说的想要的。 -
那么您要准确创建哪个输出?显示最小的示例输入文件和您要创建的相应(HTML?)结果/。
-
@MartinHonnen,我正在尝试获得与此类似的输出:仅包含文本“部分名称”的标题,然后与每个部分名称对应。理想情况下,它应该有两列,另外一列包含有关从中提取部分名称的文件的信息。
-
请编辑您的问题并显示您的示例应生成的确切代码。
-
嗨@michael.hor257k,非常感谢您的回答。您建议的两个输出都很棒,我可以使用它们。但是,我的文件没有得到转换。我没有收到任何错误,所以我不确定出了什么问题。我尝试在 Notepad++ 中使用初始帖子中的“report.xml”文件。如果我将完整路径添加到 Firefox,它只显示:“部分名称”。你知道可能出了什么问题吗?