XSLT:根据以下兄弟值返回结果

时间:2013-04-06 08:41:55

标签: xml xslt xpath

我是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时返回'value'。如果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

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分:0)

更短,更简单(只有三个模板,没有xsl:apply-templates,没有xsl:variable,没有current()和没有模式)以及更完全“推送式”的可理解解决方案“:

<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>