使用XSLT,如何从输出到单个文件中的多个xml文件中获取属性?
我有以下情况:在多个结构几乎相似的xml文件中,我需要提取一个属性及其来源的文件使用XSLT,如何从输出到单个文件中的多个xml文件中获取属性?,xml,xslt,Xml,Xslt,我有以下情况:在多个结构几乎相似的xml文件中,我需要提取一个属性及其来源的文件 <xsl:template match="/report"> <html> <body> <h1> <xsl:value-of select="title"/> </h1> <table>
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我相信我已接近解决方案,但无法使其发挥作用。我不熟悉xml和xslt,需要一些指导。多谢各位
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我的多个xml文件都位于同一位置,它们具有以下结构:
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我怀疑您想做以下事情:
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<xsl:for-each select="configfile">
<h2>
<xsl:value-of select="@name"/>
</h2>
<xsl:for-each select="document(@name)/erpConnector/sections/section">
<p>
<xsl:value-of select="@name"/>
</p>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
在您的示例中,会产生如下结果:
<html>
<body>
<h1>Section names</h1>
<h2>myFirstXMLfile.xml</h2>
<p>Section1</p>
<p>Section2</p>
<p>Section3</p>
<h2>mySecondXMLfile.xml</h2>
<p>Section1b</p>
<p>Section2b</p>
<p>Section3b</p>
</body>
</html>
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
节名
myFirstXMLfile.xml
第一节
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
第2节
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
第三节
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
mySecondXMLfile.xml
第1B节
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
第2B节
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
第3B节
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如果您希望输出一个表,请尝试:
<xsl:template match="/report">
<html>
<body>
<h1>
<xsl:value-of select="title"/>
</h1>
<table>
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<xsl:for-each select="configfile">
<xsl:variable name="filename" select="@name" />
<xsl:for-each select="document($filename)/erpConnector/sections/section">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="$filename"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
节名
文件名
您有两个具有相同匹配模式的模板。只有最后一个正在应用。由于您的第一个模板将模板应用于erpConnector
,因此您的其他模板应匹配此模板。-请注意,当前编写的模板试图输出文档中第一节的名称-这不是您所说的您想要的。那么您想要确切地创建哪个输出?显示最少的示例输入文件和您想要创建的相应(HTML?)结果/@MartinHonnen,我试图获得类似的输出:一个标题,只有文本“Section name”,然后是每个Section name的行。理想情况下,它将有两列,另外一列包含有关从中提取节名的文件的信息。请编辑您的问题并显示您的示例应生成的确切代码。您好@michael.hor257k,非常感谢您的回答。你建议的两个输出都很好,我可以使用它们。但是,我的文件不会被转换。我没有收到任何错误,所以我不确定出了什么问题。我尝试了Notepad++,使用了第一篇文章中的“report.xml”文件。如果我将完整路径添加到firefox,它只会显示:“节名”。你知道会出什么问题吗?