1

输入:

<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 值存在,则应拆分状态,否则状态为可用。我为 current 和 count 创建了变量,如果 count > 0 我将状态设置为 1,这似乎比需要的步骤多,更重要的是,2,它不起作用。

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

我做错了什么计数?

4

1 回答 1

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>

二、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>
于 2013-03-24T03:38:53.047 回答