简体   繁体   中英

XSLT 1.0 - Use delimiter to split fields inside for-each loop

I have an XML file which needs transformation. I was able to build the XSLT file which worked in the first scenario without delimiter but now in the second scenario, there is a delimiter + field which needs to split into two fields.

Here is the source XML in my second scenario; the delimeter + is on the "Group/Group/Details/Section/Field/'FieldName='{STATION.Comments}'/value" field:

<?xml version="1.0" encoding="UTF-8"?>
<CrystalReport xmlns="urn:crystal-reports:schemas:report-detail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:crystal-reports:schemas:report-detail http://www.businessobjects.com/products/xml/CR2008Schema.xsd">
   <Group Level="1">
      <GroupHeader>
         <Section SectionNumber="0">
            <Field FieldName="{STATION.Item}" Name="Field5">
               <FormattedValue>TR-BP169-10</FormattedValue>
               <Value>TR-BP169-10</Value>
            </Field>
            <Text Name="Text30">
               <TextValue>Item</TextValue>
            </Text>
            <Field FieldName="{INVENTRY.Description2}" Name="Field7">
                <FormattedValue>TRW Automotive</FormattedValue>
                <Value>TRW Automotive</Value>
            </Field>
         </Section>
      </GroupHeader>
      <Group Level="2">
         <Details Level="3">
            <Section SectionNumber="0">
               <Field FieldName="{STATION.Quantity}" Name="Field9">
                  <FormattedValue>11</FormattedValue>
                  <Value>11</Value>
               </Field>
               <Field FieldName="{STATION.Comments}" Name="Field23">
                    <FormattedValue>SWBP169-10</FormattedValue>
                    <Value>SWBP169-10+EA</Value>
                </Field>
            </Section>
         </Details>
      </Group>
      <GroupFooter>
         <Section SectionNumber="0">
            <Field FieldName="Sum ({STATION.Quantity}, {STATION.Item})" Name="Field24">
               <FormattedValue>47</FormattedValue>
               <Value>47.00</Value>
            </Field>
         </Section>
      </GroupFooter>
   </Group>
   <Group Level="1">
      <GroupHeader>
         <Section SectionNumber="0">
            <Field FieldName="{STATION.Item}" Name="Field5">
               <FormattedValue>TR-Y19CFC</FormattedValue>
               <Value>TR-Y19CFC</Value>
            </Field>
            <Text Name="Text30">
               <TextValue>Item</TextValue>
            </Text>
            <Field FieldName="{INVENTRY.Description2}" Name="Field7">
                <FormattedValue>TRW Automotive</FormattedValue>
                <Value>TRW Automotive</Value>
            </Field>
         </Section>
      </GroupHeader>
      <Group Level="2">
         <Details Level="3">
            <Section SectionNumber="0">
               <Field FieldName="{STATION.Quantity}" Name="Field9">
                  <FormattedValue>9</FormattedValue>
                  <Value>9</Value>
               </Field>
               <Field FieldName="{STATION.Comments}" Name="Field23">
                    <FormattedValue>Y19CFC</FormattedValue>
                    <Value>Y19CFC+EA</Value>
                </Field>
            </Section>
         </Details>
      </Group>
      <GroupFooter>
         <Section SectionNumber="0">
            <Field FieldName="Sum ({STATION.Quantity}, {STATION.Item})" Name="Field24">
               <FormattedValue>18</FormattedValue>
               <Value>18.00</Value>
            </Field>
         </Section>
      </GroupFooter>
   </Group>
</CrystalReport>

Here is my XSLT 1.0 file in first scenario where no delimeter was added:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cr="urn:crystal-reports:schemas:report-detail"
exclude-result-prefixes="cr msxsl cs"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:cs="urn:cs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[
    public string datenow()
    {
        return(DateTime.Now.ToString("yyyy'-'MM'-'dd"));
    }
    ]]>
</msxsl:script>

<xsl:template match="/cr:CrystalReport">
    <Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
        <Header>
            <Company>tgs</Company>
            <Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
        </Header>
        <Body>
            <MessageParts>
                <SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
                        <SalesTable class="entity">
                            <CustAccount>                               
                                <xsl:value-of select="cr:Group/cr:GroupHeader/cr:Section/cr:Field[@FieldName='{INVENTRY.Description2}']/cr:Value"/>                             
                            </CustAccount>
                        <PurchOrderFormNum>PO</PurchOrderFormNum>
                        <ReceiptDateRequested><xsl:value-of select="cs:datenow()"/></ReceiptDateRequested>                  
                        <!-- sale lines -->                                             
                            <xsl:for-each select="cr:Group">
                                <SalesLine class="entity">                          
                                    <ItemId>
                                        <xsl:value-of select="cr:Group/cr:Details/cr:Section/cr:Field[@FieldName='{STATION.Comments}']/cr:Value"/>
                                    </ItemId>              
                                    <SalesQty>
                                        <xsl:value-of select="cr:GroupFooter/cr:Section/cr:Field[@FieldName='Sum ({STATION.Quantity}, {STATION.Item})']/cr:FormattedValue"/>                   
                                    </SalesQty> 
                                    <SalesUnit>EA</SalesUnit>                 
                                </SalesLine>
                            </xsl:for-each> 
                        </SalesTable>               
                </SalesOrder>
            </MessageParts>
        </Body>
    </Envelope>
</xsl:template>

</xsl:stylesheet>

This is the desired output with separate fields using the delimiter. Delimiter use to create two fields in one for itemid and second for SalesUNIT:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
    <Header>
        <Company>tgs</Company>
        <Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
    </Header>
    <Body>
        <MessageParts>
            <SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
                <SalesTable class="entity">
                    <CustAccount>TRW Automotive</CustAccount>
                    <PurchOrderFormNum>PO</PurchOrderFormNum>
                    <ReceiptDateRequested>2019-08-01</ReceiptDateRequested>
                    <SalesLine class="entity">
                        <ItemId>SWBP169-10</ItemId>
                        <SalesQty>47</SalesQty>
                        <SalesUnit>EA</SalesUnit>
                    </SalesLine>
                    <SalesLine class="entity">
                        <ItemId>Y19CFC</ItemId>
                        <SalesQty>18</SalesQty>
                        <SalesUnit>EA</SalesUnit>
                    </SalesLine>
                </SalesTable>
            </SalesOrder>
        </MessageParts>
    </Body>
</Envelope>

Thanks in advance.

If the source will always contain two values separated by + , you can extract the first value using:

substring-before(Value, '+')

and the second by:

substring-after(Value, '+')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM