سؤال

How can you ensure the order of the output element in an XSLT?

I have an XSLT, used in a BizTalk map, which extracts the three dates Ok, but because of the order of the source data, the resulting XML is ShipmentDate, ScheduledDeliveryDate and then DocumentIssueDate.

<Dates>
  <xsl:for-each select="s0:E2EDT13001GRP">
    <xsl:variable name="qualifier" select="string(s0:E2EDT13001/s0:QUALF/text())" />
    <xsl:variable name="isDocumentIssueDate" select="string(userCSharp:LogicalEq($qualifier , &quot;015&quot;))" />
    <xsl:variable name="isSheduledDeliveryDate" select="string(userCSharp:LogicalEq($qualifier , &quot;007&quot;))" />
    <xsl:variable name="isShipmentDate" select="string(userCSharp:LogicalEq($qualifier , &quot;003&quot;))" />
    <xsl:variable name="date" select="s0:E2EDT13001/s0:NTANF/text()" />

    <xsl:if test="$isDocumentIssueDate='true'">
      <DocumentIssueDate>
        <xsl:value-of select="$date" />
      </DocumentIssueDate>
    </xsl:if>

    <xsl:if test="$isScheduledDeliveryDate='true'">
      <ScheduledDeliveryDate>
        <xsl:value-of select="$date" />
      </ScheduledDeliveryDate>
    </xsl:if>

    <xsl:if test="$isShipmentDate='true'">
      <ShipmentDate>
        <xsl:value-of select="$date" />
      </ShipmentDate>
    </xsl:if>

  </xsl:for-each>
</Dates>

When I test the map in Visual Studio, I get an error that the XML is invalid versus the XSD ...

<xs:complexType>
  <xs:sequence>
    <xs:element name="DocumentIssueDate" type="xs:string" />
    <xs:element name="SheduledDeliveryDate" type="xs:string" />
    <xs:element name="ShipmentDate" type="xs:string" />
  </xs:sequence>
</xs:complexType>

So how can I output the dates in the "right order"?

هل كانت مفيدة؟

المحلول

Rather than using for-each, just pull out the bits you need one by one:

<Dates>
  <DocumentIssueDate>
    <xsl:value-of select="s0:E2EDT13001GRP[
       userCSharp:LogicalEq(s0:E2EDT13001/s0:QUALF, '015')]
        /s0:E2EDT13001/s0:NTANF" />
  </DocumentIssueDate>
  <ScheduledDeliveryDate>
    <xsl:value-of select="s0:E2EDT13001GRP[
       userCSharp:LogicalEq(s0:E2EDT13001/s0:QUALF, '007')]
        /s0:E2EDT13001/s0:NTANF" />
  </ScheduledDeliveryDate>
  <!-- etc. -->
</Dates>

نصائح أخرى

You have two options:

  1. Does the destination type have to be a sequence? You can change it to an xs:all.
  2. Change where you're looping. You would have to enclose the for-each on E2EDT13001GRP within three separate element definitions, on each for DocumentIssueDate, SheduledDeliveryDate and ShipmentDate. That way, the output would always appear in that order.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top