仅在类似元素的第一个块上对元素进行硬编码

时间:2013-09-09 08:13:44

标签: xml xslt

我有这个XML输入

<?xml version="1.0" encoding="utf-8"?>
<Document>
  <TopLevel>
    <Header>
      <!-- Header Information-->
    </Header>
    <!--Payments is a one to many-->
    <Payments>
      <PaymentID>PID1</PaymentID>
      <!--More Info-->
      <!--Transactrion can be one to many for each payment-->
      <Transaction>
        <TranssID>TR1 - PID1</TranssID>
        <TranssID>TR2 - PID1</TranssID>
        <!--More Info-->
      </Transaction>
    </Payments>
    <Payments>
      <PaymentID>PID2</PaymentID>
      <!--More Info-->
      <!--Transactrion can be one to many for each payment-->
      <Transaction>
        <TranssID>TR1 - PID2</TranssID>
        <TranssID>TR2 - PID2</TranssID>
        <!--More Info-->
      </Transaction>
    </Payments>
  </TopLevel>
</Document>

这是我的XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <TopLevel>
      <xsl:apply-templates select="Document/TopLevel/Payments"/>
    </TopLevel>
  </xsl:template>

  <xsl:template match="Payments">
    <Payments>
      <PaymentID>
        <xsl:value-of select="PaymentID"/>
      </PaymentID>
      <ExtraColumn>
        <xsl:value-of select ="'ISO'" />
      </ExtraColumn>
      <xsl:apply-templates select="Transaction"/>
    </Payments>
  </xsl:template>

  <xsl:template match="Transaction">
    <xsl:apply-templates select="TranssID"/>
  </xsl:template>

  <xsl:template match="TranssID">
    <transIDs>
      <xsl:value-of select="."/>
    </transIDs>
  </xsl:template>

</xsl:stylesheet>

创建此输出

<?xml version="1.0" encoding="utf-8"?>
<TopLevel>
  <Payments>
    <PaymentID>PID1</PaymentID>
    <ExtraColumn>ISO</ExtraColumn>
    <transIDs>TR1 - PID1</transIDs>
    <transIDs>TR2 - PID1</transIDs>
  </Payments>
  <Payments>
    <PaymentID>PID2</PaymentID>
    <ExtraColumn>ISO</ExtraColumn>
    <transIDs>TR1 - PID2</transIDs>
    <transIDs>TR2 - PID2</transIDs>
  </Payments>
</TopLevel>

我的问题是,我只需要在第一个付款冻结块上显示ExtraColumn,不应该在后续付款冻结块上显示。

关于如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:0)

而不是

  <xsl:template match="Payments">
    <Payments>
      <PaymentID>
        <xsl:value-of select="PaymentID"/>
      </PaymentID>
      <ExtraColumn>
        <xsl:value-of select ="'ISO'" />
      </ExtraColumn>
      <xsl:apply-templates select="Transaction"/>
    </Payments>
  </xsl:template>

使用

  <xsl:template match="Payments[1]">
    <Payments>
      <PaymentID>
        <xsl:value-of select="PaymentID"/>
      </PaymentID>
      <ExtraColumn>
        <xsl:value-of select ="'ISO'" />
      </ExtraColumn>
      <xsl:apply-templates select="Transaction"/>
    </Payments>
  </xsl:template>

  <xsl:template match="Payments[position() &gt; 1]">
    <Payments>
      <PaymentID>
        <xsl:value-of select="PaymentID"/>
      </PaymentID>

      <xsl:apply-templates select="Transaction"/>
    </Payments>
  </xsl:template>

答案 1 :(得分:0)

我在下面得到了正确的答案

  <xsl:template match="Payments">
    <Payments>
      <PaymentID>
        <xsl:value-of select="PaymentID"/>
      </PaymentID>
      <xsl:if test="position()=1">
        <ExtraColumn>
          <xsl:value-of select ="'ISO'" />
        </ExtraColumn>
      </xsl:if>
      <xsl:apply-templates select="Transaction"/>
    </Payments>
  </xsl:template>