计算当前值存在的次数

时间:2013-03-24 02:04:32

标签: xml xslt xpath count

输入:

<PackageInfo>
    <outerinfo>
        <myCompareInfo>12345</myCompareInfo>
    </outerInfo>
    <outerinfo>
        <myCompareInfo>67890</myCompareInfo>
        <containedInfo>12345</containedInfo>
    </outerInfo>
    <outerinfo>
        <myCompareInfo>55667</myCompareInfo>
        <containedInfo>12345-C</containedInfo>
    </outerInfo>
</PackageInfo>

输出:

<OutputInfo>
    <Package>
        <PnNum>12345</PnNum>
        <Status>split</Status>
    </Package>
    <Package>
        <PnNum>67890</PnNum>
        <Status>avail</Status>
    </Package>
    <Package>
        <PnNum>55667</PnNum>
        <Status>avail</Status>
    </Package>          
</OutputInfo>

我需要做一个计数,但我无法弄清楚正确的语法。如果当前的myCompareInfo作为containsInfo值存在,则应该分割状态,否则状态可用。我创建了当前和计数的变量,如果计数&gt; 0我设置状态但是1,这似乎比需要更多的步骤,更重要的是,2,它不起作用。

<xsl:variable name="currentVal" select="./myCompareInfo/text()" />
<xsl:variable name="splitCount" select="/count(OutputInfo[contains(@myCompareInfo,$currentSSCC)])" />

我对伯爵的错误是什么?

1 个答案:

答案 0 :(得分:0)

此转化

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

     <xsl:key name="kContained" match="myCompareInfo"
      use="boolean(/*/*/containedInfo[contains(., current())])"/>

     <xsl:template match="/*">
      <OutputInfo><xsl:apply-templates/></OutputInfo>
     </xsl:template>

     <xsl:template match="outerInfo">
      <Package><xsl:apply-templates/></Package>
     </xsl:template>

     <xsl:template match="myCompareInfo">
      <PnNum><xsl:apply-templates/></PnNum>
      <Status>available</Status>
     </xsl:template>

     <xsl:template match=
     "myCompareInfo
              [count(.|key('kContained', 'true'))
              =
               count(key('kContained', 'true'))
              ]">
      <PnNum><xsl:apply-templates/></PnNum>
      <Status>split</Status>
     </xsl:template>
     <xsl:template match="containedInfo"/>
</xsl:stylesheet>

应用于(更正格式正确)提供的XML文档时

<PackageInfo>
    <outerInfo>
        <myCompareInfo>12345</myCompareInfo>
    </outerInfo>
    <outerInfo>
        <myCompareInfo>67890</myCompareInfo>
        <containedInfo>12345</containedInfo>
    </outerInfo>
    <outerInfo>
        <myCompareInfo>55667</myCompareInfo>
        <containedInfo>12345-C</containedInfo>
    </outerInfo>
</PackageInfo>

会产生想要的正确结果:

<OutputInfo>
   <Package>
      <PnNum>12345</PnNum>
      <Status>split</Status>
   </Package>
   <Package>
      <PnNum>67890</PnNum>
      <Status>available</Status>
   </Package>
   <Package>
      <PnNum>55667</PnNum>
      <Status>available</Status>
   </Package>
</OutputInfo>

<强> II。 XSLT 2.0解决方案:

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

 <xsl:key name="kContained" match="myCompareInfo"
  use="boolean(/*/*/containedInfo[contains(., current())])"/>

 <xsl:template match="/*">
  <OutputInfo><xsl:apply-templates/></OutputInfo>
 </xsl:template>

 <xsl:template match="myCompareInfo">
  <PnNum><xsl:apply-templates/></PnNum>
  <Status>available</Status>
 </xsl:template>

 <xsl:template match=
 "myCompareInfo[. intersect key('kContained', true())]">
  <PnNum><xsl:apply-templates/></PnNum>
  <Status>split</Status>
 </xsl:template>

 <xsl:template match="outerInfo">
  <Package><xsl:apply-templates/></Package>
 </xsl:template>
 <xsl:template match="containedInfo"/>
</xsl:stylesheet>