gpt4 book ai didi

xml - XSLT 1.0 使用基于父属性的键进行分组

转载 作者:数据小太阳 更新时间:2023-10-29 02:46:12 25 4
gpt4 key购买 nike

我需要使用 XSLT 1.0 对此进行转换:

<form>
<question NumOfColumns="3">
<title>Colors</title>
<answer>red</answer>
<answer>orange</answer>
<answer>yellow</answer>
<answer>green</answer>
<answer>blue</answer>
<answer>indigo</answer>
<answer>violet</answer>
</question>

</form>

进入这个:

<h2 class="question">Colors</h2>
<div class="answersrow">
<input type="checkbox" name="colors" value="red" id="red" /> <label for="red">red</label>
<input type="checkbox" name="colors" value="orange" id="orange" /> <label for="orange">orange</label>
<input type="checkbox" name="colors" value="yellow" id="yellow" /> <label for="yellow">yellow</label>
</div>
<div class="answersrow">
<input type="checkbox" name="colors" value="green" id="green" /> <label for="green">green</label>
<input type="checkbox" name="colors" value="blue" id="blue" /> <label for="blue">blue</label>
<input type="checkbox" name="colors" value="indigo" id="indigo" /> <label for="indigo">indigo</label>
</div>
<div class="answersrow">
<input type="checkbox" name="colors" value="green" id="green" /> <label for="green">green</label>
</div>

问题节点中的 NumOfColumns 告诉输出答案 div 时使用多少列。对于每个节点,我可以使用以下方法获取其行:

天花板(位置()div parent::*/@NumOfColumns)

这工作正常;我可以输出正确的整数。但是我无法让键/分组正常工作,我不确定问题出在哪里。

我认为关键是:

<xsl:key name="answersrow" match="form/question/answer[ceiling( position() div parent::*/@NumOfColumns) = parent::*/@NumOfColumns]" use="." />

然后我可以通过以下方式检索节点:

<xsl:for-each select="key('answersrow', answer)">

运气不好。有人有解决办法吗?还是这在 XSLT 1.0 中不可行?

最佳答案

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>

<xsl:template match="question">
<h2 class="{local-name()}">
<xsl:apply-templates select="title"/>
</h2>
<!--Select the answers that will begin the groups.
Apply templates in mode group and pass in the number of columns -->
<xsl:variable name="cols" select="@NumOfColumns"/>
<xsl:apply-templates select="answer[position() mod $cols = 1]"
mode="group" >
<xsl:with-param name="cols" select="$cols"/>
</xsl:apply-templates>
</xsl:template>

<!--Group the answers as children of the div -->
<xsl:template match="answer" mode="group">
<xsl:param name="cols"/>
<div class="answersrow">
<xsl:apply-templates
select=".|following-sibling::answer[position() &lt; $cols]"/>
</div>
</xsl:template>

<!--normal rendering for each answer -->
<xsl:template match="answer">
<input type="checkbox" name="colors" value="{.}" id="{.}" />
<label for="{.}">
<xsl:value-of select="."/>
</label>
</xsl:template>

</xsl:stylesheet>

关于xml - XSLT 1.0 使用基于父属性的键进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9651395/

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