【问题标题】:xslt count following recordsxslt 计数以下记录
【发布时间】:2017-08-01 13:26:57
【问题描述】:

使用 XSLT 1.0,我需要编写“跟随” SegmentHeader 的 Txn 记录计数。我可以在每个 SegmentHeader 下写入的 Txn 记录的数量受到限制。例如 - 如果我总共有 5 条交易记录,每个段限制为 3 条,则它们应该分为 2 个 SegmentHeaders,分别保存 3 条和 2 条交易记录。

所需输出:

<file>
    <SegmentHeader>
        <TransactionCount>3</TransactionCount>
    </SegmentHeader>
    <Txn />
    <Txn />
    <Txn />
    <SegmentHeader>
        <TransactionCount>2</TransactionCount>
    </SegmentHeader>
    <Txn />
    <Txn />
</file>

通过使用“position() mod $recordLimit = 1”,我可以根据需要同时写入 SegmentHeaders 和 Txn 记录,但无法找到写入事务计数的方法。

【问题讨论】:

  • 这里没有足够的信息。请提供一个 input XML 的示例,并包含您当前在编写 XSLT 时所做的努力。

标签: batch-file xslt count


【解决方案1】:

这里有一个简单的例子,您可以根据自己的情况进行调整:

XML

<input>
    <item>a</item>
    <item>b</item>
    <item>c</item>
    <item>d</item>
    <item>e</item>
</input>

XSLT 1.0

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

<xsl:param name="group-size" select="3"/>

<xsl:template match="/input">
    <output>
        <xsl:for-each select="item[position() mod $group-size = 1]">
            <xsl:variable name="group"  select=". | following-sibling::item[position() &lt; $group-size]"/>
            <header>
                <item-count>
                    <xsl:value-of select="count($group)"/>
                </item-count>
            </header>
            <xsl:copy-of select="$group"/>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <header>
      <item-count>3</item-count>
   </header>
   <item>a</item>
   <item>b</item>
   <item>c</item>
   <header>
      <item-count>2</item-count>
   </header>
   <item>d</item>
   <item>e</item>
</output>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多