【发布时间】:2013-12-11 18:48:08
【问题描述】:
我有一个由 2 个不同的 xml 文件组成的 1 个大 xml 文件。 我想要做的是,当我创建一个 CSV 文件时,我希望能够从文件的第二部分中获取与第一部分匹配的数据。 我使用 for each 循环遍历文件第一部分中的所有数据,然后使用嵌套循环来获取匹配的数据。 这是 XML 布局:
<?xml version="1.0" encoding="UTF-8"?>
<Report_Data>
<Report_Entry>
<company_id>001</company_id>
<dept>TestDept</dept>
</Report_Entry>
<Report_Entry>
</Report_Entry>
<Company_Data>
<Report_Entry>
<code>1</code>
<name>Test1</name>
</Report_Entry>
<Report_Entry>
<code>2</code>
<name>Test2</name>
</Report_Entry>
</Company_Data>
</Report_Data>
我希望在我的 CSV 中将来自 Company_Data 节点的名称插入到输出中,紧跟在 company_id 之后。
这是我的 XSL 文件(它目前不起作用,它循环并为输出文件中的每个公司数据节点输出 N/A):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="urn:com.workday/bsvc">
<xsl:output method="text" encoding="utf-8" media-type="text/plain" />
<xsl:strip-space elements="*"/>
<xsl:variable name="newline" select="'
'" />
<xsl:variable name="tab" select="'	'" />
<xsl:variable name="comma" select="','" />
<xsl:variable name="padding" select="' '" />
<xsl:variable name="ID" select="concat('COMPANY ID', $padding)" />
<xsl:variable name="NAME" select="concat('COMPANY NAME', $padding)" />
<xsl:variable name="DEPT" select="concat('DEPARTMENT', $padding)" />
<xsl:template match="/">
<xsl:value-of select="$ID"/><xsl:value-of select="$comma"/>
<xsl:value-of select="$NAME"/><xsl:value-of select="$comma"/>
<xsl:value-of select="$DEPT"/><xsl:value-of select="$comma"/>
<xsl:for-each select="/Report_Data/Report_Entry">
<xsl:value-of select="translate(substring(concat(company_id, $padding), 1, string-length($ID)), ',', ' ')" /><xsl:value-of select="$comma"/>
<xsl:call-template name="company_name">
<xsl:with-param name="id_number" select="company_id"></xsl:with-param>
</xsl:call-template>
<xsl:value-of select="translate(substring(concat(dept, $padding), 1, string-length($DEPT)), ',', ' ')" /><xsl:value-of select="$comma"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="company_name" match="/wd:Report_Data/wd:Company_Data">
<xsl:param name="id_number"></xsl:param>
<xsl:for-each select="/wd:Report_Entry">
<xsl:if test="contains($id_number, code)">
<xsl:value-of select="/name/text()"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我的输出如下所示:
1, TestDept
代替:
1, Test1, TestDept
它没有输出name 节点的值。
如何让它显示值?
如果需要更多信息,我可以添加内容。
【问题讨论】: