如何使用XSLT…变量格式化XML?循环?
我是XSLT新手,必须将1.0版与XPath 1.0一起使用,非常感谢您在这方面的帮助。我尝试了不同的方法,虽然我已经接近解决方案,但我仍然错过了一些东西。我已经尝试过变量,但这些变量不起作用,因为您无法重用它们,并且我无法在XML/XPath 1中创建有意义的数组。我尝试过循环,但我不希望每次都重复列标题 关于输出/数据的格式: 它基本上是一个与一些个人相关的清单(在这个例子中只有两个…可能是一个或多个) 对于第一个客户-标题基本上是“申请人:”,在第一列中紧跟申请人的姓名,在第二列中紧跟“状态”。下一行是第一列中要检查的项目,第二列中是状态。请注意,两个人要检查的项目相同(在本例中),但状态不同 这种模式对一些客户重复(这里也有2个客户,但可以是任意数量)。下面我提供了我正在使用的适用XML和XSLT文件的一小部分 这是我的XML:如何使用XSLT…变量格式化XML?循环?,xslt,Xslt,我是XSLT新手,必须将1.0版与XPath 1.0一起使用,非常感谢您在这方面的帮助。我尝试了不同的方法,虽然我已经接近解决方案,但我仍然错过了一些东西。我已经尝试过变量,但这些变量不起作用,因为您无法重用它们,并且我无法在XML/XPath 1中创建有意义的数组。我尝试过循环,但我不希望每次都重复列标题 关于输出/数据的格式: 它基本上是一个与一些个人相关的清单(在这个例子中只有两个…可能是一个或多个) 对于第一个客户-标题基本上是“申请人:”,在第一列中紧跟申请人的姓名,在第二列中紧跟“状
<?xml version="1.0" encoding="UTF-8"?>
<LendingChecklistReport>
<LoanHdr>
...
</LoanHdr>
<ChecklistItems>
<Item>
<ChecklistType>SomeListType</ChecklistType>
<ChecklistSubTypeDesc>Alpha</ChecklistSubTypeDesc>
</Item>
<Item>
<ChecklistType>Applicant</ChecklistType>
<ChecklistSubTypeDesc>Cosmo Kramer</ChecklistSubTypeDesc>
<ItemDescription>Item to Check1</ItemDescription>
<Status>Pending</Status>
</Item>
<Item>
<ChecklistType>Applicant</ChecklistType>
<ChecklistSubTypeDesc>Adolph Credco</ChecklistSubTypeDesc>
<ItemDescription>Item to Check1</ItemDescription>
<Status>Waived</Status>
</Item>
<Item>
<ChecklistType>Applicant</ChecklistType>
<ChecklistSubTypeDesc>Cosmo Kramer</ChecklistSubTypeDesc>
<ItemDescription>Item to Check2</ItemDescription>
<Status>In Progress</Status>
</Item>
<Item>
<ChecklistType>Applicant</ChecklistType>
<ChecklistSubTypeDesc>Adolph Credco</ChecklistSubTypeDesc>
<ItemDescription>Item to Check2</ItemDescription>
<Status>Holding</Status>
</Item>
....
</ChecklistItems>
:
再次感谢Thanx的帮助……在提供了答案和链接后,我想出了一个解决方案。使用提供的相同XML,以下是我使用的XSLT:
<xsl:key name="ApplicantNameCollection" match="/LendingChecklistReport/ChecklistItems/Item" use="ChecklistSubTypeDesc" />
<xsl:template match="/LendingChecklistReport">
<xsl:for-each select="ChecklistItems">
<table>
<xsl:for-each select="ChecklistItems/Item[ChecklistType='Applicant'][count(. | key('ApplicantNameCollection', ChecklistSubTypeDesc)[1]) = 1]">
<tr>
<td>Applicant: <xsl:value-of select="ChecklistSubTypeDesc" /></td>
<td>Status</td>
</tr>
<xsl:for-each select="key('ApplicantNameCollection', ChecklistSubTypeDesc)">
<tr>
<td><xsl:value-of select="ItemDescription" /></td>
<td><xsl:value-of select="Status" /></td>
</tr>
</xsl:for-each>
<tr>
<td class="Separator"></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>
申请人:
地位
请编辑您的问题并添加更有意义的标题。“我如何让这个XSLT工作”可能会让您的问题被否决并结束。描述你的实际问题是什么。分组节点?格式化输出?别忘了包含XSLT!我建议您从这里开始:您还没有向我们展示您的XSLT代码。请阅读和。您需要在发布前尝试解决方案,并提出具体问题。就目前而言,你的问题更像是“请为我做这项工作”。@helderdarocha:对不起,我会修改标题使其更具体。另外,我没有包括XSLT,因为它不起作用……那么它有什么用呢?有些事情我做得不对,所以我提出的XSLT只会混淆问题。我确实添加了一个描述(称之为伪代码……我见过其他人也这么做。@Jim Garrison:你是对的,我不想让这个问题听起来像是一种施舍。我说过我已经尝试了许多不同的方法来解决这个问题。我想我本可以复制并粘贴一段代码,但这只会使问题变得混乱,延长痛苦。在我发布之前,我确实阅读了这两个链接,并认为我的文章展示了所有必需的特性。谢谢-我一直很忙,但我很快就能看到并实现解决方案。谢谢你的回复。我能够使用你的示例和上面的示例链接正确格式化我的代码。我从来没有er在您提供的任何更高级条件之前使用了关键元素。
Applicant: Cosmo Kramer Status
Item to Check1 Pending
Item to Check2 In Progress
Applicant: Adolph Credco Status
Item to Check1 Waived
Item to Check2 Holding
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="Applicants" use="ChecklistSubTypeDesc" match="Item"/>
<xsl:template match="ChecklistItems">
<xsl:apply-templates select="Item[ChecklistType='Applicant']"/>
</xsl:template>
<xsl:template match="Item">
<xsl:variable name="currentName" select="ChecklistSubTypeDesc"/>
<xsl:if test="not(following::Item/ChecklistSubTypeDesc[text() = $currentName])">
<xsl:value-of select="ChecklistType"/>: <xsl:value-of select="$currentName"/><xsl:text> </xsl:text><xsl:value-of select="Status/name()"/>
<xsl:for-each select="key('Applicants',$currentName)"><xsl:text>
</xsl:text><xsl:apply-templates select="ItemDescription"/><xsl:text> </xsl:text><xsl:apply-templates select="Status"/>
</xsl:for-each>
</xsl:if><xsl:text>
</xsl:text>
</xsl:template>
<xsl:key name="ApplicantNameCollection" match="/LendingChecklistReport/ChecklistItems/Item" use="ChecklistSubTypeDesc" />
<xsl:template match="/LendingChecklistReport">
<xsl:for-each select="ChecklistItems">
<table>
<xsl:for-each select="ChecklistItems/Item[ChecklistType='Applicant'][count(. | key('ApplicantNameCollection', ChecklistSubTypeDesc)[1]) = 1]">
<tr>
<td>Applicant: <xsl:value-of select="ChecklistSubTypeDesc" /></td>
<td>Status</td>
</tr>
<xsl:for-each select="key('ApplicantNameCollection', ChecklistSubTypeDesc)">
<tr>
<td><xsl:value-of select="ItemDescription" /></td>
<td><xsl:value-of select="Status" /></td>
</tr>
</xsl:for-each>
<tr>
<td class="Separator"></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>