动态增加节点值

发布于 2024-11-04 14:28:06 字数 3464 浏览 0 评论 0原文

我有一个输入 xml,其结构如下:

<VEN>
<CUS>
    <MEM>
    <MEM>
    <MEM>
</CUS>
<CUS>
    <MEM>
    <MEM>
    <MEM>
</CUS>

每个节点在输入 xml 中出现多次。每个MEM中都有一个属性@CLM01 必须根据 MEM 节点每 20 次出现的情况为其分配一个值。从第一个 VEN/CUS 开始,如果 MEM 有 55 个节点,则前 20 个节点的 @CLM01 值为“1”,对于相同的 VEN/CUS 组合,接下来的 20 个节点的值为“2”,其余 15 个节点的值为“2”。 @CLM01 值为“3”。

对于同一 VEN 下的下一个 CUS(假设该 CUS 有 30 个 MEM 节点),前 20 个 MEM 节点的 @CLM01 值为“4”,接下来的 10 个 MEM 的 @CLM01 值为“5”。这样继续下去......我希望我清楚我想要实现的目标。

谢谢。是否可以?使用 xslt 1.0

这是示例输入

<Data>
<VEN vendorId= "v1">
    <CUS custId = "c1">
        <MEM memId="m1" CLM01=""/>
        <MEM memId="m2" CLM01=""/>
        <MEM memId="m3" CLM01=""/>
        <MEM memId="m4" CLM01=""/>
        <MEM memId="m5" CLM01=""/>
    </CUS>
    <CUS custId = "c2">
        <MEM memId="m11" CLM01=""/>
        <MEM memId="m12" CLM01=""/>
        <MEM memId="m13" CLM01=""/>
        <MEM memId="m14" CLM01=""/>
        <MEM memId="m15" CLM01=""/>
        <MEM memId="m16" CLM01=""/>
        <MEM memId="m17" CLM01=""/>
        <MEM memId="m18" CLM01=""/>
    </CUS>
</VEN>
<VEN vendorId= "v2">
    <CUS custId = "c1">
        <MEM memId="m4" CLM01=""/>
        <MEM memId="m5" CLM01=""/>
        <MEM memId="m6" CLM01=""/>
        <MEM memId="m7" CLM01=""/>
        <MEM memId="m8" CLM01=""/>
    </CUS>
    <CUS custId = "c1">
        <MEM memId="m33" CLM01=""/>
        <MEM memId="m44" CLM01=""/>
        <MEM memId="m55" CLM01=""/>
        <MEM memId="m66" CLM01=""/>
        <MEM memId="m77" CLM01=""/>
        <MEM memId="m88" CLM01=""/>
        <MEM memId="m99" CLM01=""/>
        <MEM memId="m11" CLM01=""/>
    </CUS>
</VEN>

期望的输出(而不是 MEM 节点出现 20 次,我们每出现 3 次就出现一次)

<Data>
<VEN vendorId= "v1">
    <CUS custId = "c1">
        <MEM memId="m1" CLM01="1"/>
        <MEM memId="m2" CLM01="1"/>
        <MEM memId="m3" CLM01="1"/>
        <MEM memId="m4" CLM01="2"/>
        <MEM memId="m5" CLM01="2"/>
    </CUS>
    <CUS custId = "c2">
        <MEM memId="m11" CLM01="3"/>
        <MEM memId="m12" CLM01="3"/>
        <MEM memId="m13" CLM01="3"/>
        <MEM memId="m14" CLM01="4"/>
        <MEM memId="m15" CLM01="4"/>
        <MEM memId="m16" CLM01="4"/>
        <MEM memId="m17" CLM01="5"/>
        <MEM memId="m18" CLM01="5"/>
    </CUS>
</VEN>
<VEN vendorId= "v2">
    <CUS custId = "c1">
        <MEM memId="m4" CLM01="6"/>
        <MEM memId="m5" CLM01="6"/>
        <MEM memId="m6" CLM01="6"/>
        <MEM memId="m7" CLM01="7"/>
        <MEM memId="m8" CLM01="7"/>
    </CUS>
    <CUS custId = "c1">
        <MEM memId="m33" CLM01="8"/>
        <MEM memId="m44" CLM01="8"/>
        <MEM memId="m55" CLM01="8"/>
        <MEM memId="m66" CLM01="9"/>
        <MEM memId="m77" CLM01="9"/>
        <MEM memId="m88" CLM01="9"/>
        <MEM memId="m99" CLM01="10"/>
        <MEM memId="m11" CLM01="10"/>
    </CUS>
</VEN>

I have an input xml which has the following structure,

<VEN>
<CUS>
    <MEM>
    <MEM>
    <MEM>
</CUS>
<CUS>
    <MEM>
    <MEM>
    <MEM>
</CUS>

each node occurs multiple times in input xml. There is an attribute @CLM01 in every MEM
node which has to be assigned a value depending upon every 20 occurences of MEM nodes. Starting from the first VEN/CUS, if there are 55 nodes of MEM then first 20 nodes will have @CLM01 value of '1' and for same VEN/CUS combination the next 20 nodes will have value '2' and remaining 15 nodes will have @CLM01 value '3'.

And for the next CUS(assuming this CUS has 30 MEM nodes) under the Same VEN the first twenty MEM nodes will have @CLM01 value of '4' and next ten MEM will have @CLM01 with value '5'. This goes on..... I hope i am clear what i am trying to achieve.

Thanks. is it possible? using xslt 1.0

Here is the sample input

<Data>
<VEN vendorId= "v1">
    <CUS custId = "c1">
        <MEM memId="m1" CLM01=""/>
        <MEM memId="m2" CLM01=""/>
        <MEM memId="m3" CLM01=""/>
        <MEM memId="m4" CLM01=""/>
        <MEM memId="m5" CLM01=""/>
    </CUS>
    <CUS custId = "c2">
        <MEM memId="m11" CLM01=""/>
        <MEM memId="m12" CLM01=""/>
        <MEM memId="m13" CLM01=""/>
        <MEM memId="m14" CLM01=""/>
        <MEM memId="m15" CLM01=""/>
        <MEM memId="m16" CLM01=""/>
        <MEM memId="m17" CLM01=""/>
        <MEM memId="m18" CLM01=""/>
    </CUS>
</VEN>
<VEN vendorId= "v2">
    <CUS custId = "c1">
        <MEM memId="m4" CLM01=""/>
        <MEM memId="m5" CLM01=""/>
        <MEM memId="m6" CLM01=""/>
        <MEM memId="m7" CLM01=""/>
        <MEM memId="m8" CLM01=""/>
    </CUS>
    <CUS custId = "c1">
        <MEM memId="m33" CLM01=""/>
        <MEM memId="m44" CLM01=""/>
        <MEM memId="m55" CLM01=""/>
        <MEM memId="m66" CLM01=""/>
        <MEM memId="m77" CLM01=""/>
        <MEM memId="m88" CLM01=""/>
        <MEM memId="m99" CLM01=""/>
        <MEM memId="m11" CLM01=""/>
    </CUS>
</VEN>

The Desired output (instead of 20 occurences of MEM nodes we go for every 3 occurences)

<Data>
<VEN vendorId= "v1">
    <CUS custId = "c1">
        <MEM memId="m1" CLM01="1"/>
        <MEM memId="m2" CLM01="1"/>
        <MEM memId="m3" CLM01="1"/>
        <MEM memId="m4" CLM01="2"/>
        <MEM memId="m5" CLM01="2"/>
    </CUS>
    <CUS custId = "c2">
        <MEM memId="m11" CLM01="3"/>
        <MEM memId="m12" CLM01="3"/>
        <MEM memId="m13" CLM01="3"/>
        <MEM memId="m14" CLM01="4"/>
        <MEM memId="m15" CLM01="4"/>
        <MEM memId="m16" CLM01="4"/>
        <MEM memId="m17" CLM01="5"/>
        <MEM memId="m18" CLM01="5"/>
    </CUS>
</VEN>
<VEN vendorId= "v2">
    <CUS custId = "c1">
        <MEM memId="m4" CLM01="6"/>
        <MEM memId="m5" CLM01="6"/>
        <MEM memId="m6" CLM01="6"/>
        <MEM memId="m7" CLM01="7"/>
        <MEM memId="m8" CLM01="7"/>
    </CUS>
    <CUS custId = "c1">
        <MEM memId="m33" CLM01="8"/>
        <MEM memId="m44" CLM01="8"/>
        <MEM memId="m55" CLM01="8"/>
        <MEM memId="m66" CLM01="9"/>
        <MEM memId="m77" CLM01="9"/>
        <MEM memId="m88" CLM01="9"/>
        <MEM memId="m99" CLM01="10"/>
        <MEM memId="m11" CLM01="10"/>
    </CUS>
</VEN>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难愈 2024-11-11 14:28:06

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pSize" select="3"/>


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

 <xsl:template match="/">
  <xsl:variable name="vrtfPass1">
   <xsl:apply-templates/>
  </xsl:variable>

  <xsl:variable name="vPass2" select=
   "ext:node-set($vrtfPass1)"/>

   <xsl:apply-templates select="$vPass2/*" mode="pass2"/>
 </xsl:template>

 <xsl:template match=
  "MEM[not(following-sibling::MEM)]">
   <xsl:call-template name="identity"/>

   <xsl:variable name="vPos" select=
   "count(preceding-sibling::MEM)+1"/>

   <xsl:variable name="pTimes"
        select="$pSize - ($vPos mod $pSize)"/>

   <xsl:call-template name="genMems">
    <xsl:with-param name="pTimes" select="$pTimes"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template name="genMems">
  <xsl:param name="pTimes"/>

  <xsl:if test="$pTimes >0">
   <MEM fake="yes"/>

   <xsl:call-template name="genMems">
    <xsl:with-param name="pTimes" select="$pTimes -1"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>

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

 <xsl:template match="MEM/@CLM01" mode="pass2">
  <xsl:attribute name="CLM01">
   <xsl:variable name="vNum">
    <xsl:number count="MEM" level="any"/>
   </xsl:variable>

    <xsl:value-of select=
      "ceiling($vNum div $pSize)"/>
   </xsl:attribute>
  </xsl:template>

  <xsl:template match="MEM[@fake='yes']" mode="pass2"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<Data>
    <VEN vendorId= "v1">
        <CUS custId = "c1">
            <MEM memId="m1" CLM01=""/>
            <MEM memId="m2" CLM01=""/>
            <MEM memId="m3" CLM01=""/>
            <MEM memId="m4" CLM01=""/>
            <MEM memId="m5" CLM01=""/>
        </CUS>
        <CUS custId = "c2">
            <MEM memId="m11" CLM01=""/>
            <MEM memId="m12" CLM01=""/>
            <MEM memId="m13" CLM01=""/>
            <MEM memId="m14" CLM01=""/>
            <MEM memId="m15" CLM01=""/>
            <MEM memId="m16" CLM01=""/>
            <MEM memId="m17" CLM01=""/>
            <MEM memId="m18" CLM01=""/>
        </CUS>
    </VEN>
    <VEN vendorId= "v2">
        <CUS custId = "c1">
            <MEM memId="m4" CLM01=""/>
            <MEM memId="m5" CLM01=""/>
            <MEM memId="m6" CLM01=""/>
            <MEM memId="m7" CLM01=""/>
            <MEM memId="m8" CLM01=""/>
        </CUS>
        <CUS custId = "c1">
            <MEM memId="m33" CLM01=""/>
            <MEM memId="m44" CLM01=""/>
            <MEM memId="m55" CLM01=""/>
            <MEM memId="m66" CLM01=""/>
            <MEM memId="m77" CLM01=""/>
            <MEM memId="m88" CLM01=""/>
            <MEM memId="m99" CLM01=""/>
            <MEM memId="m11" CLM01=""/>
        </CUS>
    </VEN>
</Data>

产生所需的正确结果:

<Data>
   <VEN vendorId="v1">
      <CUS custId="c1">
         <MEM memId="m1" CLM01="1"/>
         <MEM memId="m2" CLM01="1"/>
         <MEM memId="m3" CLM01="1"/>
         <MEM memId="m4" CLM01="2"/>
         <MEM memId="m5" CLM01="2"/>
      </CUS>
      <CUS custId="c2">
         <MEM memId="m11" CLM01="3"/>
         <MEM memId="m12" CLM01="3"/>
         <MEM memId="m13" CLM01="3"/>
         <MEM memId="m14" CLM01="4"/>
         <MEM memId="m15" CLM01="4"/>
         <MEM memId="m16" CLM01="4"/>
         <MEM memId="m17" CLM01="5"/>
         <MEM memId="m18" CLM01="5"/>
      </CUS>
   </VEN>
   <VEN vendorId="v2">
      <CUS custId="c1">
         <MEM memId="m4" CLM01="6"/>
         <MEM memId="m5" CLM01="6"/>
         <MEM memId="m6" CLM01="6"/>
         <MEM memId="m7" CLM01="7"/>
         <MEM memId="m8" CLM01="7"/>
      </CUS>
      <CUS custId="c1">
         <MEM memId="m33" CLM01="8"/>
         <MEM memId="m44" CLM01="8"/>
         <MEM memId="m55" CLM01="8"/>
         <MEM memId="m66" CLM01="9"/>
         <MEM memId="m77" CLM01="9"/>
         <MEM memId="m88" CLM01="9"/>
         <MEM memId="m99" CLM01="10"/>
         <MEM memId="m11" CLM01="10"/>
      </CUS>
   </VEN>
</Data>

说明

  1. 两遍变换。

  2. 第一遍添加假 MEM 元素,以便结果中的每个 CUS 元素都有 MEM 子元素,其数量是 的倍数>$pSize

  3. 第二遍:

  4. 使用和覆盖身份规则

  5. 使用< /code>使用 level="any" 获取 MEM 元素的正确编号。

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pSize" select="3"/>


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

 <xsl:template match="/">
  <xsl:variable name="vrtfPass1">
   <xsl:apply-templates/>
  </xsl:variable>

  <xsl:variable name="vPass2" select=
   "ext:node-set($vrtfPass1)"/>

   <xsl:apply-templates select="$vPass2/*" mode="pass2"/>
 </xsl:template>

 <xsl:template match=
  "MEM[not(following-sibling::MEM)]">
   <xsl:call-template name="identity"/>

   <xsl:variable name="vPos" select=
   "count(preceding-sibling::MEM)+1"/>

   <xsl:variable name="pTimes"
        select="$pSize - ($vPos mod $pSize)"/>

   <xsl:call-template name="genMems">
    <xsl:with-param name="pTimes" select="$pTimes"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template name="genMems">
  <xsl:param name="pTimes"/>

  <xsl:if test="$pTimes >0">
   <MEM fake="yes"/>

   <xsl:call-template name="genMems">
    <xsl:with-param name="pTimes" select="$pTimes -1"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>

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

 <xsl:template match="MEM/@CLM01" mode="pass2">
  <xsl:attribute name="CLM01">
   <xsl:variable name="vNum">
    <xsl:number count="MEM" level="any"/>
   </xsl:variable>

    <xsl:value-of select=
      "ceiling($vNum div $pSize)"/>
   </xsl:attribute>
  </xsl:template>

  <xsl:template match="MEM[@fake='yes']" mode="pass2"/>
</xsl:stylesheet>

when applied on the provided XML document:

<Data>
    <VEN vendorId= "v1">
        <CUS custId = "c1">
            <MEM memId="m1" CLM01=""/>
            <MEM memId="m2" CLM01=""/>
            <MEM memId="m3" CLM01=""/>
            <MEM memId="m4" CLM01=""/>
            <MEM memId="m5" CLM01=""/>
        </CUS>
        <CUS custId = "c2">
            <MEM memId="m11" CLM01=""/>
            <MEM memId="m12" CLM01=""/>
            <MEM memId="m13" CLM01=""/>
            <MEM memId="m14" CLM01=""/>
            <MEM memId="m15" CLM01=""/>
            <MEM memId="m16" CLM01=""/>
            <MEM memId="m17" CLM01=""/>
            <MEM memId="m18" CLM01=""/>
        </CUS>
    </VEN>
    <VEN vendorId= "v2">
        <CUS custId = "c1">
            <MEM memId="m4" CLM01=""/>
            <MEM memId="m5" CLM01=""/>
            <MEM memId="m6" CLM01=""/>
            <MEM memId="m7" CLM01=""/>
            <MEM memId="m8" CLM01=""/>
        </CUS>
        <CUS custId = "c1">
            <MEM memId="m33" CLM01=""/>
            <MEM memId="m44" CLM01=""/>
            <MEM memId="m55" CLM01=""/>
            <MEM memId="m66" CLM01=""/>
            <MEM memId="m77" CLM01=""/>
            <MEM memId="m88" CLM01=""/>
            <MEM memId="m99" CLM01=""/>
            <MEM memId="m11" CLM01=""/>
        </CUS>
    </VEN>
</Data>

produces the wanted, correct result:

<Data>
   <VEN vendorId="v1">
      <CUS custId="c1">
         <MEM memId="m1" CLM01="1"/>
         <MEM memId="m2" CLM01="1"/>
         <MEM memId="m3" CLM01="1"/>
         <MEM memId="m4" CLM01="2"/>
         <MEM memId="m5" CLM01="2"/>
      </CUS>
      <CUS custId="c2">
         <MEM memId="m11" CLM01="3"/>
         <MEM memId="m12" CLM01="3"/>
         <MEM memId="m13" CLM01="3"/>
         <MEM memId="m14" CLM01="4"/>
         <MEM memId="m15" CLM01="4"/>
         <MEM memId="m16" CLM01="4"/>
         <MEM memId="m17" CLM01="5"/>
         <MEM memId="m18" CLM01="5"/>
      </CUS>
   </VEN>
   <VEN vendorId="v2">
      <CUS custId="c1">
         <MEM memId="m4" CLM01="6"/>
         <MEM memId="m5" CLM01="6"/>
         <MEM memId="m6" CLM01="6"/>
         <MEM memId="m7" CLM01="7"/>
         <MEM memId="m8" CLM01="7"/>
      </CUS>
      <CUS custId="c1">
         <MEM memId="m33" CLM01="8"/>
         <MEM memId="m44" CLM01="8"/>
         <MEM memId="m55" CLM01="8"/>
         <MEM memId="m66" CLM01="9"/>
         <MEM memId="m77" CLM01="9"/>
         <MEM memId="m88" CLM01="9"/>
         <MEM memId="m99" CLM01="10"/>
         <MEM memId="m11" CLM01="10"/>
      </CUS>
   </VEN>
</Data>

Explanation:

  1. Two pass transformation.

  2. The first pass adds fake MEM elements so that each CUS element in the result has MEM children whose number is a multiple of $pSize.

  3. The second pass:

  4. Using and overriding the identity rule.

  5. Using <xsl:number> to get the correct numbering of MEM elements with level="any".

屋顶上的小猫咪 2024-11-11 14:28:06

只是为了好玩,一次转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pSize" select="3"/>
    <xsl:template match="node()|@*">
        <xsl:param name="pOffset" select="0"/>
        <xsl:copy>
            <xsl:apply-templates select="node()|@*">
                <xsl:with-param name="pOffset" select="$pOffset"/>
                <xsl:with-param name="pPosition" select="position()"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="VEN|CUS">
        <xsl:param name="pOffset" select="0"/>
        <xsl:variable name="vIsVEN" select="self::VEN"/>
        <xsl:copy>
            <xsl:apply-templates
             select="@*|node()[position()=1 and $vIsVEN or not($vIsVEN)]">
                <xsl:with-param name="pOffset" select="$pOffset"/>
            </xsl:apply-templates>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]">
            <xsl:with-param name="pOffset"
             select="$pOffset + count(.//MEM[position() mod $pSize = 1])"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="MEM/@CLM01">
        <xsl:param name="pPosition" select="1"/>
        <xsl:param name="pOffset" select="0"/>
        <xsl:attribute name="{name()}">
            <xsl:value-of 
             select="$pOffset + ceiling(($pPosition - 1) div $pSize)"/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

输出:

<Data>
    <VEN vendorId="v1">
        <CUS custId="c1">
            <MEM memId="m1" CLM01="1"></MEM>
            <MEM memId="m2" CLM01="1"></MEM>
            <MEM memId="m3" CLM01="1"></MEM>
            <MEM memId="m4" CLM01="2"></MEM>
            <MEM memId="m5" CLM01="2"></MEM>
        </CUS>
        <CUS custId="c2">
            <MEM memId="m11" CLM01="3"></MEM>
            <MEM memId="m12" CLM01="3"></MEM>
            <MEM memId="m13" CLM01="3"></MEM>
            <MEM memId="m14" CLM01="4"></MEM>
            <MEM memId="m15" CLM01="4"></MEM>
            <MEM memId="m16" CLM01="4"></MEM>
            <MEM memId="m17" CLM01="5"></MEM>
            <MEM memId="m18" CLM01="5"></MEM>
        </CUS>
    </VEN>
    <VEN vendorId="v2">
        <CUS custId="c1">
            <MEM memId="m4" CLM01="6"></MEM>
            <MEM memId="m5" CLM01="6"></MEM>
            <MEM memId="m6" CLM01="6"></MEM>
            <MEM memId="m7" CLM01="7"></MEM>
            <MEM memId="m8" CLM01="7"></MEM>
        </CUS>
        <CUS custId="c1">
            <MEM memId="m33" CLM01="8"></MEM>
            <MEM memId="m44" CLM01="8"></MEM>
            <MEM memId="m55" CLM01="8"></MEM>
            <MEM memId="m66" CLM01="9"></MEM>
            <MEM memId="m77" CLM01="9"></MEM>
            <MEM memId="m88" CLM01="9"></MEM>
            <MEM memId="m99" CLM01="10"></MEM>
            <MEM memId="m11" CLM01="10"></MEM>
        </CUS>
    </VEN>
    <VEN vendorId="v2">
        <CUS custId="c1">
            <MEM memId="m4" CLM01="1"></MEM>
            <MEM memId="m5" CLM01="1"></MEM>
            <MEM memId="m6" CLM01="1"></MEM>
            <MEM memId="m7" CLM01="2"></MEM>
            <MEM memId="m8" CLM01="2"></MEM>
        </CUS>
        <CUS custId="c1">
            <MEM memId="m33" CLM01="3"></MEM>
            <MEM memId="m44" CLM01="3"></MEM>
            <MEM memId="m55" CLM01="3"></MEM>
            <MEM memId="m66" CLM01="4"></MEM>
            <MEM memId="m77" CLM01="4"></MEM>
            <MEM memId="m88" CLM01="4"></MEM>
            <MEM memId="m99" CLM01="5"></MEM>
            <MEM memId="m11" CLM01="5"></MEM>
        </CUS>
    </VEN>
</Data>

注意:混合子节点和兄弟节点遍历。隧道参数。

Just for fun, one pass transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pSize" select="3"/>
    <xsl:template match="node()|@*">
        <xsl:param name="pOffset" select="0"/>
        <xsl:copy>
            <xsl:apply-templates select="node()|@*">
                <xsl:with-param name="pOffset" select="$pOffset"/>
                <xsl:with-param name="pPosition" select="position()"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="VEN|CUS">
        <xsl:param name="pOffset" select="0"/>
        <xsl:variable name="vIsVEN" select="self::VEN"/>
        <xsl:copy>
            <xsl:apply-templates
             select="@*|node()[position()=1 and $vIsVEN or not($vIsVEN)]">
                <xsl:with-param name="pOffset" select="$pOffset"/>
            </xsl:apply-templates>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]">
            <xsl:with-param name="pOffset"
             select="$pOffset + count(.//MEM[position() mod $pSize = 1])"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="MEM/@CLM01">
        <xsl:param name="pPosition" select="1"/>
        <xsl:param name="pOffset" select="0"/>
        <xsl:attribute name="{name()}">
            <xsl:value-of 
             select="$pOffset + ceiling(($pPosition - 1) div $pSize)"/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

Output:

<Data>
    <VEN vendorId="v1">
        <CUS custId="c1">
            <MEM memId="m1" CLM01="1"></MEM>
            <MEM memId="m2" CLM01="1"></MEM>
            <MEM memId="m3" CLM01="1"></MEM>
            <MEM memId="m4" CLM01="2"></MEM>
            <MEM memId="m5" CLM01="2"></MEM>
        </CUS>
        <CUS custId="c2">
            <MEM memId="m11" CLM01="3"></MEM>
            <MEM memId="m12" CLM01="3"></MEM>
            <MEM memId="m13" CLM01="3"></MEM>
            <MEM memId="m14" CLM01="4"></MEM>
            <MEM memId="m15" CLM01="4"></MEM>
            <MEM memId="m16" CLM01="4"></MEM>
            <MEM memId="m17" CLM01="5"></MEM>
            <MEM memId="m18" CLM01="5"></MEM>
        </CUS>
    </VEN>
    <VEN vendorId="v2">
        <CUS custId="c1">
            <MEM memId="m4" CLM01="6"></MEM>
            <MEM memId="m5" CLM01="6"></MEM>
            <MEM memId="m6" CLM01="6"></MEM>
            <MEM memId="m7" CLM01="7"></MEM>
            <MEM memId="m8" CLM01="7"></MEM>
        </CUS>
        <CUS custId="c1">
            <MEM memId="m33" CLM01="8"></MEM>
            <MEM memId="m44" CLM01="8"></MEM>
            <MEM memId="m55" CLM01="8"></MEM>
            <MEM memId="m66" CLM01="9"></MEM>
            <MEM memId="m77" CLM01="9"></MEM>
            <MEM memId="m88" CLM01="9"></MEM>
            <MEM memId="m99" CLM01="10"></MEM>
            <MEM memId="m11" CLM01="10"></MEM>
        </CUS>
    </VEN>
    <VEN vendorId="v2">
        <CUS custId="c1">
            <MEM memId="m4" CLM01="1"></MEM>
            <MEM memId="m5" CLM01="1"></MEM>
            <MEM memId="m6" CLM01="1"></MEM>
            <MEM memId="m7" CLM01="2"></MEM>
            <MEM memId="m8" CLM01="2"></MEM>
        </CUS>
        <CUS custId="c1">
            <MEM memId="m33" CLM01="3"></MEM>
            <MEM memId="m44" CLM01="3"></MEM>
            <MEM memId="m55" CLM01="3"></MEM>
            <MEM memId="m66" CLM01="4"></MEM>
            <MEM memId="m77" CLM01="4"></MEM>
            <MEM memId="m88" CLM01="4"></MEM>
            <MEM memId="m99" CLM01="5"></MEM>
            <MEM memId="m11" CLM01="5"></MEM>
        </CUS>
    </VEN>
</Data>

Note: Mixing children and siblings traversal. Tunneling parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文