如何使用XSLT将元素作为第n条记录插入

时间:2017-04-25 14:42:35

标签: xslt soa

考虑以下输入:

<SettleList>
<SettleObject>
  <ExternalVar>5</ExternalVar>
 </SettleObject>
 <SettleObject>
  <ExternalVar>10</ExternalVar>
</SettleObject>
</SettleList>

现在要求:除第5和第10之外的所有记录都需要为零,而第5和第10将包含值。 问题是使用apply-templates不能发送第二个对象的参数,因此索引再次从1而不是第6个记录开始。

我正在尝试使用XSLT

3 个答案:

答案 0 :(得分:1)

您的XML似乎没有正确形成一个开始。我不明白为什么一个简单的递归模板无法做到这一点。

在我的例子中,我使用了输入数据:

<SettleList>
    <SettleObject>
        <ExternalVar>3</ExternalVar>
    </SettleObject>
    <SettleObject>
        <ExternalVar>5</ExternalVar>
    </SettleObject>
    <SettleObject>
        <ExternalVar>10</ExternalVar>
    </SettleObject>
    <SettleObject>
        <ExternalVar>14</ExternalVar>
    </SettleObject>
</SettleList>

运行此脚本时:

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

<xsl:template match="SettleObject[ExternalVar=5]">
    <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="SettleObject[ExternalVar=10]">
    <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="SettleObject">
    <SettleObject>
        <ExternalVar>0</ExternalVar>
    </SettleObject>
</xsl:template>

我得到了这个输出:

<SettleList>
    <SettleObject>
        <ExternalVar>0</ExternalVar>
    </SettleObject>
    <SettleObject>
        <ExternalVar>5</ExternalVar>
    </SettleObject>
    <SettleObject>
        <ExternalVar>10</ExternalVar>
    </SettleObject>
    <SettleObject>
        <ExternalVar>0</ExternalVar>
    </SettleObject>
</SettleList>

这是你想要的输出吗?

答案 1 :(得分:1)

您没有指定XSLT版本,因此我使用的是版本2.0。

该解决方案基于(例如)5个连续的 SettleObject 标签的分组 然后插入一个额外的标签。

这里,在当前组之后,我输出了一个 ExtraObject 标签(一个例子 在前一组之后应该添加什么。)

<?xml version="1.0" encoding="utf-8"?>
<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="#all">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="SettleList">
    <xsl:copy>
      <xsl:for-each-group select="SettleObject"
        group-adjacent="xs:integer((position() - 1) div 5)">
        <xsl:for-each select="current-group()">
          <xsl:apply-templates/>
        </xsl:for-each>
        <xsl:if test="count(current-group()) = 5">
          <ExtraObject>Extra object #<xsl:value-of select="current-grouping-key() + 1"/>
          </ExtraObject>
        </xsl:if>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

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

使用Muenchian groupng在XSLT 1.0中可以实现另一种解决方案。 请参阅示例(甚至在stackoverflow上)。

答案 2 :(得分:0)

保持简单。

输入:

<SettleList xmlns="http://xmlns.oracle.com/TestApp/TestAppComposite/BPELProcess1">
   <SettleObject>
      <ExternalVar>1</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>2</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>3</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>4</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>5</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>6</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>7</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>8</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>9</ExternalVar>
   </SettleObject>
   <SettleObject>
      <ExternalVar>10</ExternalVar>
   </SettleObject>
</SettleList>

XSL:

<xsl:template match="/">
    <ns0:SettleList>
        <xsl:for-each select="/ns0:SettleList/ns0:SettleObject">
        <xsl:variable name="pos" select="position()"/>
           <xsl:choose>
           <xsl:when test="$pos = 5 or $pos = 10">
           <ns0:SettleObject>
              <ns0:ExternalVar>
                 <xsl:value-of select="ns0:ExternalVar"/>
              </ns0:ExternalVar>
           </ns0:SettleObject>
           </xsl:when>
           <xsl:otherwise>
           <ns0:SettleObject>
              <ns0:ExternalVar>
                 <xsl:text disable-output-escaping="no">0</xsl:text>
              </ns0:ExternalVar>
           </ns0:SettleObject>
           </xsl:otherwise>
           </xsl:choose>
        </xsl:for-each>
    </ns0:SettleList>
</xsl:template>

输出:

<?xml version = '1.0' encoding = 'UTF-8'?>
<ns0:SettleList xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns0="http://xmlns.oracle.com/TestApp/TestAppComposite/BPELProcess1">
   <ns0:SettleObject>
      <ns0:ExternalVar>0</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>0</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>0</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>0</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>5</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>0</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>0</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>0</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>0</ns0:ExternalVar>
   </ns0:SettleObject>
   <ns0:SettleObject>
      <ns0:ExternalVar>10</ns0:ExternalVar>
   </ns0:SettleObject>
</ns0:SettleList>