我正在尝试从 XML 数据创建 list 列表(制表符分隔的文本,用于输出)。问题是,我需要根据在 XML 中找到的数字,取出我创建的行,并多次列出它(迭代 ???)。因此,从下面的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<aisle label="AA">
<row>bb</row>
<shelf>a</shelf>
<books>4</books>
</aisle>
<aisle label="BB">
<row>cc</row>
<shelf>d</shelf>
<books>3</books>
</aisle>
</library>
我需要获取在“books”中找到的值,然后复制文本行该次数。结果看起来像这样:
Aisle Row Shelf Titles
AA bb a
AA bb a
AA bb a
AA bb a
BB cc d
BB cc d
BB cc d
以便库存盘点员可以写下在每个货架上找到的标题。我有我的 XSL 的基本结构,但我不确定如何做“迭代”部分。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="2.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:variable name="tab" select="'	'"/>
<xsl:variable name="newline" select="' '"/>
<xsl:template match="/">
<!-- Start Spreadsheet header -->
<xsl:text>Aisle</xsl:text>
<xsl:value-of select="$tab"/>
<xsl:text>Row</xsl:text>
<xsl:value-of select="$tab"/>
<xsl:text>Shelf</xsl:text>
<xsl:value-of select="$tab"/>
<xsl:text>Titles</xsl:text>
<xsl:value-of select="$newline"/>
<!-- End spreadsheet header -->
<!-- Start entering values from XML -->
<xsl:for-each select="library/aisle">
<xsl:value-of select="@label"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="row"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="shelf"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="$newline"/>
</xsl:for-each>
<!-- End of values from XML -->
<!-- Iteration of the above needed, based on count value in "books" -->
</xsl:template>
</xsl:stylesheet>
任何帮助将不胜感激。首先,“迭代”是用于此的正确术语吗?
谢谢!
最佳答案
还有一个更简单的 XSLT 2.0 非递归解决方案:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
>
<xsl:output method="text"/>
<xsl:template match="/*">
Aisle Row Shelf Titles
<xsl:text/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="aisle">
<xsl:variable name="vText" select=
"concat(@label, '	', row, '	', shelf)"
/>
<xsl:for-each select="1 to xs:integer(books)">
<xsl:value-of select="concat($vText, '
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
关于xslt - 基于计数的迭代或 XSL 中的复制。我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2822186/