XSLT增量重置元素的变化

  • 本文关键字:元素 变化 XSLT xslt-1.0
  • 更新时间 :
  • 英文 :


xml:

<?xml version="1.0" encoding="utf-8"?>
  <Projects> 
    <Project GUID="K9"> 
    <Name>Range</Name>
    <WBSs>
        <WBS GUID="2">
            <Name>Site</Name>
            <ParentWBS/>
            <ProjectGUID>K9</ProjectGUID>
            <ObjectId>99040</ObjectId>
            <Activities/>
            <WBSs>
                <WBS GUID="1">
                <Name>West</Name>
                <ParentWBS>2</ParentWBS>
                <ProjectGUID>K9</ProjectGUID>
                <ObjectId>99046</ObjectId>
                <Activities>
                  <Activity GUID="A100">
                    <Name>Task</Name>
                    <ProjectGUID>K17</ProjectGUID>
                    <WBSCode>1</WBSCode>
                  </Activity>
                </Activities>
             </WBS>
             <WBS GUID="2">
                 <Name>South</Name>
                 <ParentWBS>2</ParentWBS>
                 <ProjectGUID>K9</ProjectGUID>
                 <ObjectId>99047</ObjectId>
                 <Activities>
                   <Activity GUID="A200">
                     <Name>Task 2</Name>
                   </Activity>
                 </Activities>
             </WBS>
             <WBS GUID="3">
                 <Name>North</Name>
                 <ParentWBS>2</ParentWBS>
                 <ProjectGUID>K9</ProjectGUID>
                 <ObjectId>99048</ObjectId>
                 <Activities/>
             </WBS>
          </WBSs>
      </WBS>
    </WBSs>
    <Activities/>
  </Project>
</Projects>

这是我当前的输出:

<OBJECTID>99046</OBJECTID >
<ACTIVITY>0010</ACTIVITY>
<OBJECTID>99047</OBJECTID >
<ACTIVITY>0020</ACTIVITY>

所需的输出:

<OBJECTID>99046</OBJECTID >
<ACTIVITY>0010</ACTIVITY>
<OBJECTID>99047</OBJECTID >
<ACTIVITY>0010</ACTIVITY>

当我进行现有活动和现有objectid时,第2个条款正常工作。当没有现有当objectId或ObjectID在第三个时更改。我需要的是从" 0010"开始,并增加10个直到$ objectid更改,然后再次从" 0010"开始,依此类推。我的背景是程序编程,因此仍然习惯了做事的功能方式和不更改变量的限制。预先感谢。

这是相关的XSLT代码:

<ACTIVITY> 
<!-- count($ExistingActivity) = 1 -> ObjectId & Activity Id exist, increment $LastObjectIdActivity/ACTIVITY
   count($ExistingObjectId) = 1 -> ObjectId exists, increment $LastObjectId/ACTIVITY  
   count($ExistingObjectId) = 0 -> $ObjectId start at 0010 and keep incrementing until ObjectId change. 
-->
   <xsl:variable name="incr" select="10"/>
     <xsl:choose>
        <xsl:when test="count($ExistingActivity) = 1">      <!-- Exact match of ObjectId & Activity -->
        <!-- Get last existing ObjectId activity -->                        
          <xsl:variable name="temp" select="$LastObjectIdActivity/ACTIVITY"/>                                 
          <xsl:variable name="Oper" select="format-number($temp, '0000')"/>                              
          <xsl:value-of select="$Oper"/>
        </xsl:when>
        <xsl:when test="count($ExistingObjectId) = 1"> <!-- Activity not found, But ObjectId exists-->
        <!-- Get last existing ObjectId activity -->
          <xsl:variable name="temp" select="$LastObjectId/ACTIVITY + $incr"/>                                 
          <xsl:variable name="Oper" select="format-number($temp, '0000')"/>                              
          <xsl:value-of select="$Oper"/>
        </xsl:when>                                                                            
        <xsl:when test="count($ExistingObjectId) = 0">   <!-- No Existing ObjectId -->
          <xsl:variable name="pos" select="(position() * 10)"/>  
          <xsl:variable name="NextOper" select="format-number($pos, '0000')"/>  <!-- Start Activity at 0010 reset on new ObjectId-->
          <xsl:value-of select="$NextOper"/>
          <!-- $NextOper should be incremented by 10 -->                           
        </xsl:when> 
        <xsl:otherwise>
        </xsl:otherwise>                           
     </xsl:choose>                    
</ACTIVITY>

我认为您想要的是为每个活动分配一个值,所以这就是您要匹配的

<xsl:template match="//Activity">
<OBJECTID>
    <xsl:value-of select="../../ObjectId" />
</OBJECTID>
<ACTIVITY>
    <xsl:value-of select="format-number(10 * (count(preceding-sibling::*) +1), '0000') "/>
</ACTIVITY>
</xsl:template>

这将为输入中的每个活动提供OBJECTID/ACTIVITY对。count表达式评估了上一个同级活动的数量,即基于1个索引。

相关内容

  • 没有找到相关文章

最新更新