1

我对 XSLT 还很陌生。任何帮助,将不胜感激 ....

<Test>
  <loop loop_id="5" LoopElementOrder_no="1" Segment_id="3" >
     <usage_id>1</usage_id>
    <value>41</value>
  </loop>
  <loop loop_id="5" LoopElementOrder_no="2"  Segment_id="3">
    <usage_id>1</usage_id>
     <value>2</value>
      </loop>
  <loop loop_id="5" LoopElementOrder_no="4" Segment_id="3">
     <usage_id>2</usage_id>
     <value>raj</value>
      </loop>
  <loop loop_id="5" LoopElementOrder_no="5" Segment_id="3">
     <usage_id>3</usage_id>
      <value>d</value>
    </loop>
  <loop loop_id="5"  LoopElementOrder_no="6" Segment_id="3">
    <usage_id>3</usage_id>  
      <value>8</value>
  </loop>
  <loop loop_id="5"  LoopElementOrder_no="7" Segment_id="3">
    <usage_id>3</usage_id>
      <value>7</value>
  </loop>
  <loop loop_id="5" LoopElementOrder_no="1" Segment_id="4">
       <usage_id>1</usage_id>

    <value>IC</value>
      </loop>
  <loop loop_id="5"  LoopElementOrder_no="2" Segment_id="4">
  <usage_id>2</usage_id>
      <value>d</value> 
  </loop>
 <loop loop_id="5"  LoopElementOrder_no="3" Segment_id="4">
      <usage_id>3</usage_id>
          <value>dm</value> 
      </loop> 
 <loop loop_id="5"  LoopElementOrder_no="4" Segment_id="4">
      <usage_id>2</usage_id>
          <value>wj</value> 
      </loop> 
    </Test>

我只想在其usage_id为1或2时返回“值”。如果usage_id为3,它应该返回“N/A”。但是,例如,如果我在 Segment_id=3 LoopElementOrder_no=5 中,则 Segment_id=3 的剩余部分不包含任何 Usage_id 作为 1 或 2,在这种情况下,我需要返回“^”并使用 LoopElementOrder_no 从 Segment_id=4 继续它=1。

上述所需的输出是:

41 2 raj ^IC d N/A wj
4

2 回答 2

0

请试一试:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="loop">
    <xsl:value-of select="concat(value, ' ')"/>
  </xsl:template>

  <xsl:template match="loop[usage_id = 3]">
    <xsl:variable name="segmentId" select="@Segment_id" />
    <xsl:variable name="following12"
                  select="following-sibling::loop[@Segment_id = $segmentId]
                                                 [usage_id = 1 or usage_id = 2]" />
    <xsl:apply-templates
      select="current()[not(preceding-sibling::loop[1]
                                                   [@Segment_id = $segmentId]
                                                   [usage_id = 3])
                        and not($following12)]" mode="carat" />
    <xsl:apply-templates select="current()[$following12]" mode="na" />
  </xsl:template>

  <xsl:template match="loop" mode="na">
    <xsl:text>N/A </xsl:text>
  </xsl:template>

  <xsl:template match="loop" mode="carat">
    <xsl:text>^</xsl:text>
  </xsl:template>
</xsl:stylesheet>

在您的示例输入上运行时,结果是:

41 2 raj ^IC d N/A wj 
于 2013-04-06T14:41:47.520 回答
0

一个更短、更简单(只有三个模板, no xsl:apply-templates​​、 no xsl:variable、 nocurrent()和 no 模式)和更易于理解的解决方案,完全采用“推式”:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="loop[not(usage_id >2)]">
  <xsl:value-of select="concat(value, ' ')"/>
 </xsl:template>

 <xsl:template match=
 "loop[usage_id = 3
     and preceding-sibling::loop[not(usage_id >2)][1]/@Segment_id
        =following-sibling::loop[not(usage_id >2)][1]/@Segment_id
      ]
 ">N/A </xsl:template>

 <xsl:template match=
 "loop
   [usage_id >2
  and
    preceding-sibling::loop[1][not(usage_id >2)]
  and
    not(@Segment_id =following-sibling::loop[not(usage_id >2)][1]/@Segment_id)
    ]">^ </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<Test>
  <loop loop_id="5" LoopElementOrder_no="1" Segment_id="3" >
     <usage_id>1</usage_id>
    <value>41</value>
  </loop>
  <loop loop_id="5" LoopElementOrder_no="2"  Segment_id="3">
    <usage_id>1</usage_id>
     <value>2</value>
      </loop>
  <loop loop_id="5" LoopElementOrder_no="4" Segment_id="3">
     <usage_id>2</usage_id>
     <value>raj</value>
      </loop>
  <loop loop_id="5" LoopElementOrder_no="5" Segment_id="3">
     <usage_id>3</usage_id>
      <value>d</value>
    </loop>
  <loop loop_id="5"  LoopElementOrder_no="6" Segment_id="3">
    <usage_id>3</usage_id>
      <value>8</value>
  </loop>
  <loop loop_id="5"  LoopElementOrder_no="7" Segment_id="3">
    <usage_id>3</usage_id>
      <value>7</value>
  </loop>
  <loop loop_id="5" LoopElementOrder_no="1" Segment_id="4">
       <usage_id>1</usage_id>

    <value>IC</value>
      </loop>
  <loop loop_id="5"  LoopElementOrder_no="2" Segment_id="4">
  <usage_id>2</usage_id>
      <value>d</value>
  </loop>
 <loop loop_id="5"  LoopElementOrder_no="3" Segment_id="4">
      <usage_id>3</usage_id>
          <value>dm</value>
      </loop>
 <loop loop_id="5"  LoopElementOrder_no="4" Segment_id="4">
      <usage_id>2</usage_id>
          <value>wj</value>
      </loop>
</Test>

产生了想要的正确结果:

41 2 raj ^ IC d N/A wj

更新

该解决方案可以进一步缩短和简化:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="loop[not(usage_id >2)]">
   <xsl:if test=
   "preceding-sibling::loop[not(usage_id >2)][1]
      [not(@Segment_id = current()/@Segment_id)]">^ </xsl:if>
   <xsl:value-of select="concat(value, ' ')"/>
 </xsl:template>

 <xsl:template match=
 "loop[usage_id = 3
     and preceding-sibling::loop[not(usage_id >2)][1]/@Segment_id
        =following-sibling::loop[not(usage_id >2)][1]/@Segment_id
      ]
 ">N/A </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>
于 2013-04-06T18:36:40.623 回答