问题描述:

Please find below in more detail. Sorry I am new to this website and to XSLT

I am trying to achieve the below scenario

I have an XML with the below content for an employee

<Identifier>

<Operation>ADD</Operation>

<Position_ID>12345</Operation_ID>

<Issued_Date>2013-12-10</Issued_Date>

<Country>CN</Country>

</Identifier>

<Identifier>

<Operation>REMOVE</Operation>

<Position_ID>6734</Operation_ID>

<Issued_Date>2013-11-09</Issued_Date>

<Country>CN</Country>

</Identifier>

I am trying to output a txt file with one Operation element from Identifier section as below based on the Recent Operation activity Assuming REMOVE operation is the recent operation, I would like to output as

E001,SAM,PAUL,REMOVE,6734,2013-11-09,CN

If ADD operation is the recent activity happened then I need to output as below

E001,SAM,PAUL,ADD,12345,2013-12-10,CN

I used the below XSLT code inorder to pull the latest, but since I am using the 'or' operator it is providing me the below output which is Incorrect

<xsl:choose>

<xsl:when test="pi:Identifier[pi:Operation = 'ADD' or pi:Operation = 'REMOVE']">

<xsl:for-each select="pi:Identifier">

<Identifiers>

<Operation>

<xsl:value-of select="pi:Operation" />

</Operation>

<Position_ID><xsl:value-of select="pi:Position_ID </Position_ID

<Country><xsl:value-of select="pi:Country" /> </Country>

<Issued_Date>

<xsl:value-of select="pi:Issued_Date" />

</Issued_Date>

</Identifiers>

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

</choose>

E001,SAM,PAUL,ADD,12345,2013-12-10,CN,REMOVE,6734,2013-11-09,CN

Please let me know if this helps.

网友答案:

I am trying to output a txt file with one Operation element from Identifier section as below based on the Recent Operation activity

Assuming you are using XSLT 1.0, the way to achieve this is to sort the Identifier elements by date, and take the data from the first (or from the last, depending on the sort order) element.

For example, given a well-formed (!) input:

XML

<root>
    <Identifier>
        <Operation>ADD</Operation>
        <Position_ID>12345</Position_ID>
        <Issued_Date>2013-12-10</Issued_Date>
        <Country>CN</Country>
    </Identifier>
    <Identifier>
        <Operation>REMOVE</Operation>
        <Position_ID>6734</Position_ID>
        <Issued_Date>2013-11-09</Issued_Date>
        <Country>CN</Country>
    </Identifier>
</root>

applying the following stylesheet:

XSLT 1.0

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

<xsl:template match="/root">
    <xsl:for-each select="Identifier">
        <xsl:sort select="Issued_Date" data-type="text" order="descending"/>
        <xsl:if test="position()=1">
            <xsl:value-of select="Operation" />
            <xsl:text>,</xsl:text>
            <xsl:value-of select="Position_ID" />
            <xsl:text>,</xsl:text>
            <xsl:value-of select="Issued_Date" />
            <xsl:text>,</xsl:text>
            <xsl:value-of select="Country" />
        </xsl:if>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

will result in:

ADD,12345,2013-12-10,CN
相关阅读:
Top