我需要通过XML循环(xsl:for-each)
,它将返回x个具有相同元素名称和节点布局的节点。
我想为我遇到的每个节点和每个值创建一个可以分别寻址的逻辑参数。我尝试了数组方法,但当我寻址数组中应该对应的元素时,没有返回值,并且变量确实有值,但它看起来像一个没有任何分隔符的长字符串。
所以我想知道我是否可以创建带编号的变量名,比如variable0、variable1等。
我尝试使用<xsl:variable name="concat(name, position())" />
,但这是不允许的。
一个人知道实现这一点的方法吗?或者这是绝对不可能的吗?
感谢
XML数据:
<page id="11045138">
<name>SSC NA XML</name>
<description/>
<server>reg6699cic01</server>
<created>2013/11/18 07:16:53 PM</created>
<adhocmessage/>
<workgroups>
<workgroup>
<name>SSC_NA_BWDO_All</name>
<agents>17</agents>
<agentsavailable>2</agentsavailable>
<agentsloggedin>5</agentsloggedin>
<longestavailable>00:01:28</longestavailable>
<longestoutbound>-</longestoutbound>
<longestinbound>-</longestinbound>
<longestnonacd>-</longestnonacd>
<numbernonacd>0</numbernonacd>
<numberoninbound>0</numberoninbound>
<numberoninboundinacw>0</numberoninboundinacw>
<numberonoutbound>0</numberonoutbound>
<numberonoutboundinacw>0</numberonoutboundinacw>
</workgroup>
<workgroup>
<name>SSC_NA_DR_All</name>
<agents>10</agents>
<agentsavailable>0</agentsavailable>
<agentsloggedin>0</agentsloggedin>
<longestavailable>-</longestavailable>
<longestoutbound>-</longestoutbound>
<longestinbound>-</longestinbound>
<longestnonacd>-</longestnonacd>
<numbernonacd>0</numbernonacd>
<numberoninbound>0</numberoninbound>
<numberoninboundinacw>0</numberoninboundinacw>
<numberonoutbound>0</numberonoutbound>
<numberonoutboundinacw>0</numberonoutboundinacw>
</workgroup>
</workgroups>
</page>
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="//page/workgroups/workgroup">
<xsl:variable name="foo-elements" select="name"/>
<html>
<head>
<title>testing</title>
</head>
<body>
Body Text<br/>
<xsl:value-of select="$foo-elements[position()]"/>
</body>
</html>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我需要的是,数据将显示为一个表格,显示这些值(以及更多),就像这个一样
Data elements [0] || Data elements [0+1]
Data elements [0+2] || Data elements [0+3]
/ /
Data elements [0+m] || Data elements [0+n]
实际可能需要3行
Data elements [0] || Data elements [0+1] || Data elements [0+2]
Data elements [0+3] || Data elements [0+4] || Data elements [0+5]
/ /
Data elements [0+k] || Data elements [0+l] || Data elements [0+m]
我希望这能简要地解释我正在努力实现的目标。我有一些东西可以循环并将其放在一起,但随后信息从我们需要显示的墙板上消失。
感谢
如果您想创建一个具有一定列数的HTML表,那么您可以将其与XSLT 1.0或2.0一起使用,方法是处理workgroup[position() mod $row-count = 1]
创建行,处理. | following-sibling::workgroup[position() < $row-count]
创建单元格(以不同的模式):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="row-count" select="3"/>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//workgroups"/>
</body>
</html>
</xsl:template>
<xsl:template match="workgroups">
<table>
<thead>
<tr>
<xsl:apply-templates select="descendant::*[position() <= $row-count]" mode="th"/>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="workgroup[position() mod $row-count = 1]"/>
</tbody>
</table>
</xsl:template>
<xsl:template match="*" mode="th">
<th>
<xsl:value-of select="position()"/>
</th>
</xsl:template>
<xsl:template match="workgroup">
<tr>
<xsl:apply-templates select=". | following-sibling::workgroup[position() < $row-count]" mode="cell"/>
</tr>
</xsl:template>
<xsl:template match="workgroup" mode="cell">
<td>
<xsl:value-of select="name"/>
</td>
</xsl:template>
</xsl:stylesheet>
这改变了
<page id="11045138">
<name>SSC NA XML</name>
<description/>
<server>reg6699cic01</server>
<created>2013/11/18 07:16:53 PM</created>
<adhocmessage/>
<workgroups>
<workgroup>
<name>SSC_NA_BWDO_All</name>
<agents>17</agents>
<agentsavailable>2</agentsavailable>
<agentsloggedin>5</agentsloggedin>
<longestavailable>00:01:28</longestavailable>
<longestoutbound>-</longestoutbound>
<longestinbound>-</longestinbound>
<longestnonacd>-</longestnonacd>
<numbernonacd>0</numbernonacd>
<numberoninbound>0</numberoninbound>
<numberoninboundinacw>0</numberoninboundinacw>
<numberonoutbound>0</numberonoutbound>
<numberonoutboundinacw>0</numberonoutboundinacw>
</workgroup>
<workgroup>
<name>SSC_NA_DR_All</name>
<agents>10</agents>
<agentsavailable>0</agentsavailable>
<agentsloggedin>0</agentsloggedin>
<longestavailable>-</longestavailable>
<longestoutbound>-</longestoutbound>
<longestinbound>-</longestinbound>
<longestnonacd>-</longestnonacd>
<numbernonacd>0</numbernonacd>
<numberoninbound>0</numberoninbound>
<numberoninboundinacw>0</numberoninboundinacw>
<numberonoutbound>0</numberonoutbound>
<numberonoutboundinacw>0</numberonoutboundinacw>
</workgroup>
<workgroup>
<name>3</name>
</workgroup>
<workgroup>
<name>4</name>
</workgroup>
<workgroup>
<name>5</name>
</workgroup>
<workgroup>
<name>6</name>
</workgroup>
</workgroups>
</page>
进入
<html>
<body>
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody>
<tr>
<td>SSC_NA_BWDO_All</td>
<td>SSC_NA_DR_All</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
</table>
</body>
</html>
你不能按照你建议的方式去做。如果你告诉我们你想解决什么问题,那么我们可以告诉你如何解决。