我有来自SharePoint控件的以下XML。我想使用XSLT进行转换以生成嵌套的ul> li列表。但我遇到问题,因为我迭代每一行,主文件夹重复每一行,而不是制作一个主文件夹节点,并在该节点下添加Menu_Display_name的值来模仿树视图... 我的XML就像:
<dsQueryResponse>
<NewDataSet>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ CORP HR TIME SELF SERVICE" SECONDARY_FOLDER="Time" MENU_DISPLAY_NAME="Create Timecard"></Row>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ CORP HR TIME SELF SERVICE" SECONDARY_FOLDER="Time" MENU_DISPLAY_NAME="Recent Timecards"></Row>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ CORP HR TIME SELF SERVICE" SECONDARY_FOLDER="Time" MENU_DISPLAY_NAME="Templates"></Row>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ CORP HR TIME SELF SERVICE" SECONDARY_FOLDER="Time" MENU_DISPLAY_NAME="Timecard Search"></Row>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ CORP EXP ENTRY" SECONDARY_FOLDER="" MENU_DISPLAY_NAME="Expenses Home"></Row>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ HR EMP SELF SERVICE" SECONDARY_FOLDER="" MENU_DISPLAY_NAME="Accommodation Request"></Row>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ HR EMP SELF SERVICE" SECONDARY_FOLDER="" MENU_DISPLAY_NAME="Additional Personal Information" ></Row>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ HR EMP SELF SERVICE" SECONDARY_FOLDER="" MENU_DISPLAY_NAME="All Actions Awaiting Your Attention"></Row>
<Row DOMAIN_USERNAME="someUser" PRIMARY_FOLDER="XYZ HR EMP SELF SERVICE" SECONDARY_FOLDER="" MENU_DISPLAY_NAME="Appraisals"></Row>
</NewDataSet>
</dsQueryResponse>
我在XSL上的“穷人的尝试”是这样的:
<xsl:template name="dvt_1.body">
<xsl:param name="Rows" />
<xsl:param name="FirstRow" />
<xsl:param name="LastRow" />
<ul>
<xsl:for-each select="$Rows">
<xsl:if test="position() >= $FirstRow and position() <= $LastRow">
<xsl:call-template name="dvt_1.rowview" />
</xsl:if>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template name="dvt_1.rowview">
<li class="isFolder isExpanded">
<xsl:value-of select="@PRIMARY_FOLDER" />
<xsl:choose>
<xsl:when test="@SECONDARY_FOLDER != ''">
<ul>
<li class="isFolder isExpanded">
<xsl:value-of select="@SECONDARY_FOLDER" />
<ul>
<li><xsl:value-of select="@MENU_DISPLAY_NAME" /></li>
</ul>
</li>
</ul>
</xsl:when>
<xsl:otherwise>
<ul>
<li><xsl:value-of select="@MENU_DISPLAY_NAME" /></li>
</ul>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:template>
我想要的xsl输出是这样的:
<div id="navigator">
<ul>
<li class="isFolder isExpanded">
XYZ CORP HR TIME SELF SERVICE
<ul>
<li class="isFolder isExpanded">
Time
<ul>
<li><a href="#" target="_tab">Create Timecard</a></li>
<li><a href="#" target="_tab">Recent Timecards</a></li>
<li><a href="#" target="_tab">Templates</a></li>
<li><a href="#" target="_tab">Timecard Search</a></li>
</ul>
</li>
</ul>
</li>
<li class="isFolder isExpanded">
XYZ CORP EXP ENTRY
<ul>
<li><a href="#" target="_tab">Expense Home</a></li>
</ul>
</li>
<li class="isFolder isExpanded">
XYZ HR EMP SELF SERVICE
<ul>
<li><a href="#" target="_tab">Accommodation Request</a></li>
<li><a href="#" target="_tab">Additional Personal Information</a></li>
<li><a href="#" target="_tab">All Actions Awaiting Your Attention</a></li>
<li><a href="#" target="_tab">Appraisals</a></li>
</ul>
</li>
</ul>
</div>
有人可以帮我用xslt帮助我实现这个目标吗?
答案 0 :(得分:4)
Muenchian grouping确实是你需要在XSLT 1.0中看到的(这是Sharepoint使用的,我相信)。首先按PRIMARY_VALUE
属性进行分组,因此您有一个这样的键:
<xsl:key name="primary" match="Row" use="@PRIMARY_FOLDER" />
但我会假设您可能会为给定的SECONDARY_FOLDER
提供多个PRIMARY_VALUE
,因此您需要第二个密钥:
<xsl:key name="secondary" match="Row" use="concat(@PRIMARY_FOLDER, '|', @SECONDARY_FOLDER)" />
首先选择第一次出现PRIMARY_FOLDER
值
<xsl:for-each select="$rows[generate-id() = generate-id(key('primary', @PRIMARY_FOLDER)[1])]">
然后在其中,您选择具有不同SECONDARY_VALUE
的行以构成嵌套列表的基础
<xsl:apply-templates select="key('primary', @PRIMARY_FOLDER)[generate-id() = generate-id(key('secondary', concat(@PRIMARY_FOLDER, '|', @SECONDARY_FOLDER))[1])]" mode="secondary" />
唯一的额外工作是根据是否填充SECONDARY_FOLDER
,您的行为略有不同。您可以使用两个单独的模板执行此操作。
试试这个XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:key name="primary" match="Row" use="@PRIMARY_FOLDER" />
<xsl:key name="secondary" match="Row" use="concat(@PRIMARY_FOLDER, '|', @SECONDARY_FOLDER)" />
<xsl:variable name="rows" select="//Row" />
<xsl:template match="/">
<ul>
<xsl:for-each select="$rows[generate-id() = generate-id(key('primary', @PRIMARY_FOLDER)[1])]">
<li class="isFolder isExpanded">
<xsl:value-of select="@PRIMARY_FOLDER" />
<xsl:apply-templates select="key('primary', @PRIMARY_FOLDER)[generate-id() = generate-id(key('secondary', concat(@PRIMARY_FOLDER, '|', @SECONDARY_FOLDER))[1])]" mode="secondary" />
</li>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template match="Row[@SECONDARY_FOLDER != '']" mode="secondary">
<li class="isFolder isExpanded">
<xsl:value-of select="@SECONDARY_FOLDER" />
<ul>
<xsl:apply-templates select="key('secondary', concat(@PRIMARY_FOLDER, '|', @SECONDARY_FOLDER))" />
</ul>
</li>
</xsl:template>
<xsl:template match="Row" mode="secondary">
<xsl:apply-templates select="key('primary', @PRIMARY_FOLDER)" />
</xsl:template>
<xsl:template match="Row">
<li>
<a href="#" target="_tab">
<xsl:value-of select="@MENU_DISPLAY_NAME" />
</a>
</li>
</xsl:template>
</xsl:stylesheet>
在http://xsltransform.net/pPzifq9处查看此操作。
请注意,如果您的实际XML具有名称空间,则需要修改XSLT以将其考虑在内。