gpt4 book ai didi

javascript - 展开/折叠 xslt 中的嵌套节点

转载 作者:行者123 更新时间:2023-12-02 20:15:02 24 4
gpt4 key购买 nike

我有一个以下格式的 xml;

<BasePeriod>
.
.
.
.
<Period>
<start> stdate1 </start>
<end> endate1 </end>
<subperiod>
<substart> substart1 </substart>
<subend> subend1 </subend>
</subperiod>
<type> abc1 </type>
</Period>
<Period>
<start> stdate1 </start>
<end> endate1 </end>
<subperiod>
<substart> substart2 </substart>
<subend> subend2 </subend>
</subperiod>
<type> abc2 </type>
</Period>
<Period>
<start> stdate1 </start>
<end> endate1 </end>
<subperiod>
<substart> substart3 </substart>
<subend> subend3 </subend>
</subperiod>
<type> abc3 </type>
</Period>
</BasePeriod>

我想做的是 - 当我打开 html 时折叠期间和子期间,然后在单击它时展开它们。

我有以下 xslt:

<xsl:for-each select="SvcPeriods/BasePeriod">
<tr>
<td>
<xsl:value-of select="startDate"/>
</td>
<td>
<xsl:value-of select="endDate"/>
</td>
<td>
<a href="javascript:expandIt(per_expand{position()}), period_expand{position()}" name="period_expand{position()}" class="expandit">Periods</a>
<div id="per_expand{position()}" style="display:none;" >
<table>
<thead>
<tr>
<th>
Start Date
</th>
<th>
End Date
</th>
<th>
Sub Periods
</th>
<th>
Type
</th>
</tr>
</thead>
<xsl:for-each select="Period">
<tr>
<td>
<xsl:value-of select="start"/>
</td>
<td>
<xsl:value-of select="end"/>
</td>
<td>
<a href="javascript:expandIt(subper_expand{position()}), subperiod_expand{position()}" name="subperiod_expand{position()}" class="expandit">Sub Periods</a>
<div id="subper_expand{position()}" style="display:none;" >
<table>
<tr>
<th >
Start Date
</th>
<th >
End Date
</th>
</tr>
<xsl:for-each select="subperiod">
<tr>
<td>
<xsl:value-of select="substart"/>
</td>
<td>
<xsl:value-of select="subend"/>
</td>
</tr>
</xsl:for-each>
</table>
</div>
</td>
<td>
<xsl:value-of select="type"/>
</td>
</tr>
</xsl:for-each>
</table>
</div>
</td>
</tr>
</xsl:for-each>

<script language="JavaScript">
function expandIt(whichEl, link) {
whichEl.style.display = (whichEl.style.display == "none" ) ? "" : "none";
if ( link ) {
if ( link.innerHTML ) {
if ( whichEl.style.display == "none" ) {
link.innerHTML = "[+] " + link.innerHTML.substring( 4 );
} else {
link.innerHTML = "[-] " + link.innerHTML.substring( 4 );
}
}
}
}
</script>

通过上述内容,我无法扩展子周期。然后,我尝试将 document.getElementById(subper_expand{position()}') 作为第一个参数传递给 expandit 函数。这次我可以扩大子期间,但问题是,它始终引用第一个期间的子期间(即使我在第二个或第三个期间内单击)。

有人可以帮我吗?

谢谢!

最佳答案

将 XSL 分解为多个模板

使用多个模板而不是使用“隐藏”模板是一种很好的做法 <xsl:for-each>元素。

基期:

<xsl:template match="SvcPeriods/BasePeriod">
<tr>
<td>
<xsl:value-of select="startDate"/>
</td>
<td>
<xsl:value-of select="endDate"/>
</td>
<td>
<a href="javascript:expandIt(per_expand{position()},
per_expand{position()})"
name="period_expand{position()}" class="expandit">Periods</a>
<div id="per_expand{position()}" style="display:none;">
<table>
<tr>
<th> Start Date </th>
<th> End Date </th>
<th> Sub Periods </th>
<th> Type </th>
</tr>
<xsl:apply-templates select="Period"/>
</table>
</div>
</td>
</tr>

<xsl:call-template name="expandIt"/>
</xsl:template>

期间:

<xsl:template match="Period">
<tr>
<td>
<xsl:value-of select="start"/>
</td>
<td>
<xsl:value-of select="end"/>
</td>
<td>
<a href="javascript:expandIt(subper_expand{position()},
subperiod_expand{position()})"
name="subperiod_expand{position()}"
class="expandit">Sub Periods</a>
<div id="subper_expand{position()}" style="display:none;">
<table>
<tr>
<th> Start Date </th>
<th> End Date </th>
</tr>
<xsl:apply-templates select="subperiod"/>
</table>
</div>
</td>
<td>
<xsl:value-of select="type"/>
</td>
</tr>
</xsl:template>

子周期:

<xsl:template match="subperiod">
<tr>
<td>
<xsl:value-of select="substart"/>
</td>
<td>
<xsl:value-of select="subend"/>
</td>
</tr>
</xsl:template>

展开:

<xsl:template name="expandIt">
<script language="JavaScript">
function expandIt(whichEl, link) {
whichEl.style.display = (whichEl.style.display == "none" ) ? "" : "none";
if ( link ) {
if ( link.innerHTML ) {
if ( whichEl.style.display == "none" ) {
link.innerHTML = "[+] " + link.innerHTML.substring( 4 );
} else {
link.innerHTML = "[-] " + link.innerHTML.substring( 4 );
}
}
}
}
</script>
</xsl:template>
<小时/>

如你所见,我改变了:

<a href="javascript:expandIt(subper_expand{position()}),
subperiod_expand{position()}"

至:

<a href="javascript:expandIt(subper_expand{position()},
subperiod_expand{position()})"

(与 per_expand 相同)。结果(使用 Opera):

enter image description here

下一步(单击周期链接):

enter image description here

下一步(单击子周期):

enter image description here

看起来没问题,按预期展开和折叠工作。

关于javascript - 展开/折叠 xslt 中的嵌套节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6417148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com