xslt中的递增变量

  • 本文关键字:变量 xslt xslt xslt-1.0
  • 更新时间 :
  • 英文 :


for循环下有一个for each循环和一个if条件。如果if条件为真,我想每次都增加变量I的值。基于变量i的值,我想实现一些逻辑。下面是代码

<xsl:variable name="i" select="0"/>
<xsl:for-each select="Request/ModifyProductsAndPackages/NrcList/Nrc">
<xsl:variable name="typeIdNrcVar" select="TypeIdNrc"/>
<xsl:if test="count(/Request/WaiveNrcList/WaiveNrc/TypeIdNrc[text()=$typeIdNrcVar])=0">
   <xsl:variable name="i" select="$i + 1"/>
   <xsl:if test ="$i=1">
    do something
   </xsl:if>
</xsl:if>
</xsl:for-each/>

这里i的值不能递增。谁能建议我应该如何完成上述任务。谢谢你的帮助。

下面是xml结构
<Request>
<ModifyProductsAndPackages>
<NrcList>
   <Nrc>
     <TypeIdNrc>14046</TypeIdNrc>
     <ServiceInternalId>98602440</ServiceInternalId>
     <ServiceInternalIdResets>0</ServiceInternalIdResets>
     <ViewableOnly>1</ViewableOnly>
   </Nrc>
   <Nrc>
     <TypeIdNrc>12002</TypeIdNrc>
     <ServiceInternalId>98602440</ServiceInternalId>
     <ServiceInternalIdResets>0</ServiceInternalIdResets>
     <ViewableOnly>1</ViewableOnly>
   </Nrc>
   <Nrc>
     <TypeIdNrc>13006</TypeIdNrc>
     <ServiceInternalId>98602440</ServiceInternalId>
     <ServiceInternalIdResets>0</ServiceInternalIdResets>
     <ViewableOnly>1</ViewableOnly>
   </Nrc>
   <Nrc>
     <TypeIdNrc>14098</TypeIdNrc>
     <ServiceInternalId>98602440</ServiceInternalId>
     <ServiceInternalIdResets>0</ServiceInternalIdResets>
     <ViewableOnly>1</ViewableOnly>
   </Nrc>
 </NrcList>     
 </ModifyProductsAndPackages>
<WaiveNrcList>
<WaiveNrc>
  <TypeIdNrc>12002</TypeIdNrc>
  <ServiceInternalId>98602440</ServiceInternalId>
  <ServiceInternalIdResets>0</ServiceInternalIdResets>
</WaiveNrc>
<WaiveNrc>
  <TypeIdNrc>13256</TypeIdNrc>
  <ServiceInternalId>98602440</ServiceInternalId>
  <ServiceInternalIdResets>0</ServiceInternalIdResets>
</WaiveNrc>
<WaiveNrc>
  <TypeIdNrc>14046</TypeIdNrc>
  <ServiceInternalId>98602440</ServiceInternalId>
  <ServiceInternalIdResets>0</ServiceInternalIdResets>
</WaiveNrc>
</WaiveNrcList>
</Request>

我想要实现的最终结果是NrcList = NrcList- waivenrclist。我想在TypeIdNrc, ServiceInternalId, ServiceInternalIdResets的基础上排除匹配记录下面是结果xml

<Request>
  <NrcList>
    <Nrc>
      <TypeIdNrc>13006</TypeIdNrc>
      <ServiceInternalId>98602440</ServiceInternalId>
      <ServiceInternalIdResets>0</ServiceInternalIdResets>
<ViewableOnly>1</ViewableOnly>
</Nrc>
<Nrc>
<TypeIdNrc>14098</TypeIdNrc>
<ServiceInternalId>98602440</ServiceInternalId>
<ServiceInternalIdResets>0</ServiceInternalIdResets>
<ViewableOnly>1</ViewableOnly>
    </Nrc>
</NrcList>
</Request> 

另一种方法:

这里有一个示例,仅复制而不是WaiveNrcList中有匹配记录的Nrc元素-基于匹配TypeIdNrc:

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:strip-space elements="*"/>
<xsl:key name="waive" match="WaiveNrc" use="TypeIdNrc" />
<xsl:template match="/">
    <Request>
        <NrcList>
            <xsl:for-each select="Request/ModifyProductsAndPackages/NrcList/Nrc[not(key('waive', TypeIdNrc))]">
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </NrcList>
    </Request>
</xsl:template>
</xsl:stylesheet>

应用到你的输入示例,结果是:

<?xml version="1.0" encoding="UTF-8"?>
<Request>
   <NrcList>
      <Nrc>
         <TypeIdNrc>13006</TypeIdNrc>
         <ServiceInternalId>98602440</ServiceInternalId>
         <ServiceInternalIdResets>0</ServiceInternalIdResets>
         <ViewableOnly>1</ViewableOnly>
      </Nrc>
      <Nrc>
         <TypeIdNrc>14098</TypeIdNrc>
         <ServiceInternalId>98602440</ServiceInternalId>
         <ServiceInternalIdResets>0</ServiceInternalIdResets>
         <ViewableOnly>1</ViewableOnly>
      </Nrc>
   </NrcList>
</Request>

相关内容

  • 没有找到相关文章

最新更新