xslt查找变量中特定元素的位置
xslt finding position of a specific element in a variable
我有一个全局变量name="cats" as="element()*
。我用来创建变量的数据和我要转换的数据位于文档树的不同部分。
假设变量包含元素 CellA、CellF 和 CellC(包含在 <Item>
标记中)。
我的目标是用来自 table 的数据填充以下预定义结构,其中每个 <line>
包含与变量一样多的单元格 <field>
(并且顺序相同)。
<row>
<CellA> </CellA>
<CellB> </CellB>
<CellC> </CellC>
<CellD> </CellD>
<CellE> </CellE>
<CellF> </CellF>
</row>
我的想法是检查每个 <Cell_>
名称是否存在于变量中,并使用其位置来访问原始数据。
不幸的是,我无法在循环 $cats
时使用 position()
,因为我无法在此处访问 <field>
。
其他地方建议的另一种方法是计算 $cats = Cell_
的前兄弟姐妹的数量。但是,该建议并非基于变数。而且(由于是新手)我不知道该怎么做。
可以这样做吗?还有其他方法吗?
如果有什么不清楚的,请告诉我。
附加信息
sourceXML
<body>
<line>
<field>data</field>
<field/>
<field/>
</line>
<line/>
...
</body>
targetXML(见上文)
XSLT
<xsl:variable name="cats" as="element()*">
<Item>CellA</Item>
<Item>CellF</Item>
<Item>CellC</Item>
</xsl:variable>
<xsl:for-each select="body/line">
<row>
<CellA>
*What to do here to fill it with data from source xml*
<xsl:variable name="pos" select="???"/>
<xsl:value-of select="field[$pos]"/>
</CellA>
<CellB>
</CellB>
...
</row>
My idea is to check for each <Cell_>
if the name exists in the
variable and use its position to access the original data.
显然您想使用 index-of()
函数。
我有一个全局变量name="cats" as="element()*
。我用来创建变量的数据和我要转换的数据位于文档树的不同部分。
假设变量包含元素 CellA、CellF 和 CellC(包含在 <Item>
标记中)。
我的目标是用来自 table 的数据填充以下预定义结构,其中每个 <line>
包含与变量一样多的单元格 <field>
(并且顺序相同)。
<row>
<CellA> </CellA>
<CellB> </CellB>
<CellC> </CellC>
<CellD> </CellD>
<CellE> </CellE>
<CellF> </CellF>
</row>
我的想法是检查每个 <Cell_>
名称是否存在于变量中,并使用其位置来访问原始数据。
不幸的是,我无法在循环 $cats
时使用 position()
,因为我无法在此处访问 <field>
。
其他地方建议的另一种方法是计算 $cats = Cell_
的前兄弟姐妹的数量。但是,该建议并非基于变数。而且(由于是新手)我不知道该怎么做。
可以这样做吗?还有其他方法吗?
如果有什么不清楚的,请告诉我。
附加信息
sourceXML
<body>
<line>
<field>data</field>
<field/>
<field/>
</line>
<line/>
...
</body>
targetXML(见上文)
XSLT
<xsl:variable name="cats" as="element()*">
<Item>CellA</Item>
<Item>CellF</Item>
<Item>CellC</Item>
</xsl:variable>
<xsl:for-each select="body/line">
<row>
<CellA>
*What to do here to fill it with data from source xml*
<xsl:variable name="pos" select="???"/>
<xsl:value-of select="field[$pos]"/>
</CellA>
<CellB>
</CellB>
...
</row>
My idea is to check for each
<Cell_>
if the name exists in the variable and use its position to access the original data.
显然您想使用 index-of()
函数。