【问题标题】:Using XSLT, how to get an attribute from multiple xml files outputed into one single file?使用 XSLT,如何从输出到单个文件中的多个 xml 文件中获取属性?
【发布时间】: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,它只显示:“部分名称”。你知道可能出了什么问题吗?

标签: xml xslt


【解决方案1】:

我怀疑你想做这样的事情:

XSLT 1.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>

在您的示例中会产生如下结果:

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 2017-05-26
    • 1970-01-01
    相关资源
    最近更新 更多