2

我正在使用 xslt 生成一个文本文件。当我传递 xml 输入时,xslt 正在将 xml 输入转换为文本文件。我们能否为每个调用提供序列号。

并将其存储在某个变量中。

1)如果假设第一次执行时创建了一个文本文件,因此 xslt ( <sequence>) 中有一个变量,它应该分配为数字 1,如下所示

<sequence>1</sequence>

2)第二次执行创建了一个文本文件,因此序列变量应该增加。

<sequence>2<sequence>

3)第三次执行时,又创建了一个文本文件,因此序列变成这样

<sequence>3</sequence>

我们通常可以通过在 oracle 数据库中创建一个序列并在 xslt 中调用该序列来执行此操作,并且对于每次执行,序列都会增加

<sequence>CallOracleSequence</sequence>

任何人都可以建议不要使用 Oracle 序列。我们可以在 xslt 中处理这个吗?

4

3 回答 3

0

通常序列号将与输入中的某些内容相关,因此您可以使用position()or xsl:number。但细节取决于输入的结构。

于 2013-04-06T09:04:33.957 回答
0

XSLT 不会在转换的执行之间维护状态。

一种选择是利用包含序列号的外部配置文件。使用实体引用,您可以使 XSLT 文档的 XML 配置部分读取它的当前值,当 XSLT 执行时,增加数字并使用新的序列号覆盖配置文件<xsl:result-document>

下面是一个XSLT 2.0样式表的工作示例,假设sequence.xml在与正在执行的 XSLT 相同的目录中调用了一个序列文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--delare entities to reference the sequence file-->
<!DOCTYPE xsl:stylesheet [
<!ENTITY sequenceFile SYSTEM "sequence.xml">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output name="sequenceOutput" method="xml" indent="yes"/> 

  <!--this variable is used to store the expanded entity reference for 
      the current sequence.xml file
    When the XSLT is parsed it will "look" like this to the XML parser:
    <xsl:variable name="sequence><sequence>1</sequence></xsl:variable>
  -->
    <xsl:variable name="sequence">
        <!--
        this entity reference will expand to: 
            <sequence>x</sequence> 
        when the XSLT is parsed
        -->
        &sequenceFile;
    </xsl:variable>
   <!--
    Use the document() function with an empty value to read the XSLT 
    and then parse the sequence value produced by the entity reference
   -->
    <xsl:variable name="currentSequenceValue" 
      select="number(document('')/*/xsl:variable[@name='sequence']/sequence)"/>

    <xsl:template match="/">
        <!--do normal processing of the XML document-->
        <xsl:apply-templates />

        <!--
        This will overwrite the sequence file with the incremented value
        -->
        <xsl:result-document format="sequenceOutput" href="sequence.xml">
          <sequence><xsl:value-of select="$currentSequenceValue+1"/></sequence>
        </xsl:result-document>

    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
于 2013-04-06T18:46:28.107 回答
0

这可以通过写出两个文件(转换结果和更新的执行次数)通过单个 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 omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vRepetitions" select=
   "document('file:///c:/temp/delete/numberOfRepetitions.xml')/* +1"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
     <xsl:result-document
        href="file:///c:/temp/delete/iteration{$vRepetitions}.xml">
       <xsl:apply-templates/>
     </xsl:result-document>

     <xsl:result-document href="file:///c:/temp/delete/numberOfRepetitions.xml">
      <n><xsl:value-of select="$vRepetitions"/></n>
     </xsl:result-document>
 </xsl:template>
</xsl:stylesheet>

涉及两个文件:源 XML 文档和一个包含当前执行次数的文件——后者必须最初创建以包含

<n>0</n>

当上述转换应用于任何源 XML 文档时(对于本演示,它只是对其应用身份规则),它会进行常规处理并产生所需的结果。此外,转换读取包含当前执行次数的 XML 文档并更新此数字并将更新的(执行次数)文档写回磁盘:

Saxon 9.1.0.5J from Saxonica
Java version 1.6.0_31
Stylesheet compilation time: 625 milliseconds
Processing file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml
Building tree for file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml using class net.sf.saxon.tinytree.TinyBuilder
Tree built in 16 milliseconds
Tree size: 4 nodes, 4 characters, 0 attributes
Loading net.sf.saxon.event.MessageEmitter
Building tree for file:///c:/temp/delete/numberOfRepetitions.xml using class net.sf.saxon.tinytree.TinyBuilder
Tree built in 0 milliseconds
Tree size: 4 nodes, 1 characters, 0 attributes

Writing to file:/c:/temp/delete/iteration2.xml
Writing to file:/c:/temp/delete/numberOfRepetitions.xml

Execution time: 140 milliseconds
Memory used: 11477344
NamePool contents: 16 entries in 16 chains. 6 prefixes, 7 URIs

在这里,我们看到在第二次执行时,转换创建了两个文件

  1. iteration2.xml包含第二次执行转换的结果。

  2. numberOfRepetitions.xml如果我们检查这个文件,它在第二次执行后的内容与预期的一样:

. . . .

<n>2</n>
于 2013-04-06T19:01:40.487 回答