增加xslt中的变量

时间:2014-06-04 01:03:23

标签: xslt xslt-1.0

我有一个for循环和for循环的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> 

另一种方法:                                                                    

1 个答案:

答案 0 :(得分:1)

以下是仅根据匹配Nrc复制WaiveNrcList元素在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>