I need to do a conditional sum using XSLT. The sum of 'Oty' for each 'SKU' should be calculated only for providers listed within the 'Provider' node. In the provided example, the Qty for providerCode 4 should be skipped as its not in the 'Providers' list. I'm restricted to using XSLT 1.0.
I would appreciate any help. Thanks!
Here is the sample XML.
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Providers>
<ProviderCode>1</ProviderCode>
<ProviderCode>2</ProviderCode>
<ProviderCode>3</ProviderCode>
</Providers>
<SKU>
<SKU>XYZ</SKU>
<Description>XYZ Description</Description>
<Provider>
<ProviderCode>1</ProviderCode>
<Qty>100</Qty>
</Provider>
<Provider>
<ProviderCode>2</ProviderCode>
<Qty>67</Qty>
</Provider>
<Provider>
<ProviderCode>3</ProviderCode>
<Qty>74</Qty>
</Provider>
<Provider>
<ProviderCode>4</ProviderCode>
<Qty>62</Qty>
</Provider>
</SKU>
<SKU>
<SKU>ABC</SKU>
<Description>ABC Description</Description>
<Provider>
<ProviderCode>1</ProviderCode>
<Qty>20</Qty>
</Provider>
<Provider>
<ProviderCode>2</ProviderCode>
<Qty>77</Qty>
</Provider>
<Provider>
<ProviderCode>3</ProviderCode>
<Qty>42</Qty>
</Provider>
<Provider>
<ProviderCode>4</ProviderCode>
<Qty>631</Qty>
</Provider>
</SKU>
</Root>
Here is the required output.
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<SKU>
<SKU>XYZ</SKU>
<Qty>241</Qty>
</SKU>
<SKU>
<SKU>ABC</SKU>
<Qty>139</Qty>
</SKU>
</Root>
You can simply use sum on the nodes you want, either by comparing sum(Provider[ProviderCode = //Providers/ProviderCode]/Qty) or by using a key:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="prov" match="Providers/ProviderCode" use="."/>
<xsl:template match="Root">
<xsl:copy>
<xsl:apply-templates select="SKU"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Root/SKU">
<xsl:copy>
<xsl:copy-of select="SKU"/>
<Qty><xsl:value-of select="sum(Provider[key('prov', ProviderCode)]/Qty)"/></Qty>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Related
I have an xml that looks like below.
<?xml version="1.0" encoding="UTF-8"?>
<Invoice xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<cac:InvoiceLine>
<cbc:ID></cbc:ID>
<cbc:LineExtensionAmount currencyID="EUR">93.46</cbc:LineExtensionAmount>
<cac:Item>
<cac:AdditionalItemProperty>
<cbc:Name>Total Hours</cbc:Name>
<cbc:Value>43</cbc:Value>
</cac:AdditionalItemProperty>
<cac:AdditionalItemProperty>
<cbc:Name>naamKandidaat</cbc:Name>
<cbc:Value>Kees Netelvrees</cbc:Value>
</cac:AdditionalItemProperty>
</cac:Item>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID></cbc:ID>
<cbc:LineExtensionAmount currencyID="EUR">2.77</cbc:LineExtensionAmount>
<cac:Item>
<cac:AdditionalItemProperty>
<cbc:Name>Total Hours</cbc:Name>
<cbc:Value>43</cbc:Value>
</cac:AdditionalItemProperty>
<cac:AdditionalItemProperty>
**<cbc:Name>naamKandidaat</cbc:Name>**
<cbc:Value>Jaap Aap</cbc:Value>
</cac:AdditionalItemProperty>
</cac:Item>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID></cbc:ID>
<cbc:LineExtensionAmount currencyID="EUR">100.00</cbc:LineExtensionAmount>
<cac:Item>
<cac:AdditionalItemProperty>
<cbc:Name>Total Hours</cbc:Name>
<cbc:Value>43</cbc:Value>
</cac:AdditionalItemProperty>
<cac:AdditionalItemProperty>
**<cbc:Name>naamKandidaat</cbc:Name>**
<cbc:Value>Jaap Aap</cbc:Value>
</cac:AdditionalItemProperty>
</cac:Item>
</cac:InvoiceLine>
</Invoice>
I need to group cac:InvoiceLine/cac:Item/cac:AdditionalItemProperty/cbc:Value where InvoiceLine/cac:Item/cac:AdditionalItemProperty/cbc:Name**=naamKandidaat**.
The purpose is to sum nodes within cac:InvoiceLine where cbc:Name is the same.
The problem is that I do not know how to create the key
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:default="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" exclude-result-prefixes="cac cbc ccts qdt udt default">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:key name="PerAdditionalItemProperty" match="cac:InvoiceLine" use="cac:Item/cac:AdditionalItemProperty/cbc:Value"/>
<xsl:template match="/">
<PurchaseInvoices_version_1.0>
<xsl:for-each select="default:Invoice">
<PurchaseInvoice>
<xsl:for-each select="cac:InvoiceLine[generate-id(.)=generate-id(key('PerAdditionalItemProperty',cac:Item/cac:AdditionalItemProperty/cbc:Value)[1])]">
<xsl:for-each select="key('PerAdditionalItemProperty',cac:Item/cac:AdditionalItemProperty/cbc:Value)">
<xsl:if test="position()=1">
<Line>
<Item><xsl:value-of select="cac:Item/cac:AdditionalItemProperty/cbc:Value"/></Item>
<LineAmount><xsl:value-of select="format-number(sum(key('PerAdditionalItemProperty',cac:Item/cac:AdditionalItemProperty/cbc:Value)/cbc:LineExtensionAmount[number(.) = number(.)]),'#.##')"/></LineAmount>
</Line>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</PurchaseInvoice>
</xsl:for-each>
</PurchaseInvoices_version_1.0>
</xsl:template>
</xsl:stylesheet>
The output that I expect is:
<?xml version="1.0" encoding="utf-8"?>
<PurchaseInvoices_version_1.0>
<PurchaseInvoice>
<Line>
<Item>Kees Netelvrees</Item>
<LineAmount>93.46</LineAmount>
</Line>
<Line>
<Item>Jaap Aap</Item>
<LineAmount>102.77</LineAmount>
</Line>
</PurchaseInvoice>
</PurchaseInvoices_version_1.0
You just need to put the conditions into the key expression:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="group" match="cac:InvoiceLine" use="cac:Item/cac:AdditionalItemProperty[cbc:Name = 'naamKandidaat']/cbc:Value"/>
<xsl:template match="cac:InvoiceLine[generate-id() = generate-id(key('group', cac:Item/cac:AdditionalItemProperty[cbc:Name = 'naamKandidaat']/cbc:Value)[1])]">
<Line>
<Item>
<xsl:value-of select="cac:Item/cac:AdditionalItemProperty[cbc:Name = 'naamKandidaat']/cbc:Value"/>
</Item>
<LineAmount>
<xsl:value-of select="sum(key('group', cac:Item/cac:AdditionalItemProperty[cbc:Name = 'naamKandidaat']/cbc:Value)/cbc:LineExtensionAmount)"/>
</LineAmount>
</Line>
</xsl:template>
<xsl:template match="cac:InvoiceLine[not(generate-id() = generate-id(key('group', cac:Item/cac:AdditionalItemProperty[cbc:Name = 'naamKandidaat']/cbc:Value)[1]))]"/>
<xsl:template match="/">
<PurchaseInvoices_version_1.0>
<PurchaseInvoice>
<xsl:apply-templates/>
</PurchaseInvoice>
</PurchaseInvoices_version_1.0>
</xsl:template>
</xsl:stylesheet>
I am trying to use muenchian to do a group/sum, but I can't make it work.
Any help available?! :)
XML I have:
<RLA760910>
<G_L_By_Object_Account___Localization_S10>
<Zsz_ObjectAcctSub_OSBOW_ID3>401100.900</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>-20</SumBegBalance>
<SumDebitPeriod>10</SumDebitPeriod>
<SumCreditPeriod>-5</SumCreditPeriod>
<SumEndBalance>-15</SumEndBalance>
</G_L_By_Object_Account___Localization_S10>
<G_L_By_Object_Account___Localization_S10>
<Zsz_ObjectAcctSub_OSBOW_ID3>401100.900</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>100</SumBegBalance>
<SumDebitPeriod>10</SumDebitPeriod>
<SumCreditPeriod>-5</SumCreditPeriod>
<SumEndBalance>105</SumEndBalance>
</G_L_By_Object_Account___Localization_S10>
<Zsz_ObjectAcctSub_OSBOW_ID3>411100.900</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>-30</SumBegBalance>
<SumDebitPeriod>5</SumDebitPeriod>
<SumCreditPeriod>-10</SumCreditPeriod>
<SumEndBalance>-35</SumEndBalance>
</G_L_By_Object_Account___Localization_S10>
<Zsz_ObjectAcctSub_OSBOW_ID3>451100.900</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>80</SumBegBalance>
<SumDebitPeriod>20</SumDebitPeriod>
<SumCreditPeriod>-10</SumCreditPeriod>
<SumEndBalance>90</SumEndBalance>
</G_L_By_Object_Account___Localization_S10>
</RLA760910>
I've so far:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
<xsl:key name="AcctSub"
match="/RLA760910/G_L_By_Object_Account___Localization_S10"
use="#Zsz_ObjectAcctSub_OSBOW_ID3" />
<xsl:template match="Zsz_ObjectAcctSub_OSBOW_ID3">
<result>
<!-- Match the first acct element for a specific group -->
<xsl:apply-templates select="/RLA760910/G_L_By_Object_Account___Localization_S10/[generate-id() = generate-id(key('AcctSub', #Zsz_ObjectAcctSub_OSBOW_ID3)[1])]" />
</result>
</xsl:template>
<xsl:template match="/RLA760910/G_L_By_Object_Account___Localization_S10">
<total type="{#Zsz_ObjectAcctSub_OSBOW_ID3}">
<!-- Sum all the elements from the #type group -->
<xsl:value-of select="sum(key('AcctSub', #Zsz_ObjectAcctSub_OSBOW_ID3)/#Zsz_ObjectAcctSub_OSBOW_ID3)" />
</total>
</xsl:template>
</xsl:stylesheet>
Expected result will sum
SumBegBalance, SumDebitPeriod, SumCreditPeriod, SumEndBalance Group BY Zsz_ObjectAcctSub_OSBOW_ID3
Use the following script:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:key name="AcctSub"
match="G_L_By_Object_Account___Localization_S10"
use="Zsz_ObjectAcctSub_OSBOW_ID3"/>
<xsl:template match="RLA760910">
<xsl:copy>
<xsl:for-each select="*[generate-id()=generate-id(
key('AcctSub', Zsz_ObjectAcctSub_OSBOW_ID3)[1])]">
<result>
<xsl:variable name="Objects" select="key('AcctSub',
Zsz_ObjectAcctSub_OSBOW_ID3)"/>
<Zsz_ObjectAcctSub_OSBOW_ID3>
<xsl:value-of select="Zsz_ObjectAcctSub_OSBOW_ID3"/>
</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>
<xsl:value-of select="sum($Objects/SumBegBalance)"/>
</SumBegBalance>
<SumDebitPeriod>
<xsl:value-of select="sum($Objects/SumDebitPeriod)"/>
</SumDebitPeriod>
<SumCreditPeriod>
<xsl:value-of select="sum($Objects/SumCreditPeriod)"/>
</SumCreditPeriod>
<SumEndBalance>
<xsl:value-of select="sum($Objects/SumEndBalance)"/>
</SumEndBalance>
</result>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
A few notes:
Zsz_ObjectAcctSub_OSBOW_ID3 is not an atribute but an element, so #
is not needed.
As the name of the root tag I used the same name as in your input.
Each result output tag contains Zsz_ObjectAcctSub_OSBOW_ID3 tag
(the grouping key) and sums of your 4 tags (within the group).
Try this as your starting point:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="grp" match="G_L_By_Object_Account___Localization_S10" use="Zsz_ObjectAcctSub_OSBOW_ID3" />
<xsl:template match="/RLA760910">
<result>
<xsl:for-each select="G_L_By_Object_Account___Localization_S10[generate-id() = generate-id(key('grp', Zsz_ObjectAcctSub_OSBOW_ID3)[1])]" >
<group type="{Zsz_ObjectAcctSub_OSBOW_ID3}">
<total-of-begbalance>
<xsl:value-of select="sum(key('grp', Zsz_ObjectAcctSub_OSBOW_ID3)/SumBegBalance)"/>
</total-of-begbalance>
<!-- add more totals here-->
</group>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
When this is applied to the following well-formed example input:
XML
<RLA760910>
<G_L_By_Object_Account___Localization_S10>
<Zsz_ObjectAcctSub_OSBOW_ID3>401100.900</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>-20</SumBegBalance>
<SumDebitPeriod>10</SumDebitPeriod>
<SumCreditPeriod>-5</SumCreditPeriod>
<SumEndBalance>-15</SumEndBalance>
</G_L_By_Object_Account___Localization_S10>
<G_L_By_Object_Account___Localization_S10>
<Zsz_ObjectAcctSub_OSBOW_ID3>401100.900</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>100</SumBegBalance>
<SumDebitPeriod>10</SumDebitPeriod>
<SumCreditPeriod>-5</SumCreditPeriod>
<SumEndBalance>105</SumEndBalance>
</G_L_By_Object_Account___Localization_S10>
<G_L_By_Object_Account___Localization_S10>
<Zsz_ObjectAcctSub_OSBOW_ID3>411100.900</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>-30</SumBegBalance>
<SumDebitPeriod>5</SumDebitPeriod>
<SumCreditPeriod>-10</SumCreditPeriod>
<SumEndBalance>-35</SumEndBalance>
</G_L_By_Object_Account___Localization_S10>
<G_L_By_Object_Account___Localization_S10>
<Zsz_ObjectAcctSub_OSBOW_ID3>451100.900</Zsz_ObjectAcctSub_OSBOW_ID3>
<SumBegBalance>80</SumBegBalance>
<SumDebitPeriod>20</SumDebitPeriod>
<SumCreditPeriod>-10</SumCreditPeriod>
<SumEndBalance>90</SumEndBalance>
</G_L_By_Object_Account___Localization_S10>
</RLA760910>
the result will be:
<?xml version="1.0" encoding="utf-8"?>
<result>
<group type="401100.900">
<total-of-begbalance>80</total-of-begbalance>
</group>
<group type="411100.900">
<total-of-begbalance>-30</total-of-begbalance>
</group>
<group type="451100.900">
<total-of-begbalance>80</total-of-begbalance>
</group>
</result>
Note that your expressions using #Zsz_ObjectAcctSub_OSBOW_ID3 fail because Zsz_ObjectAcctSub_OSBOW_ID3 is an element, not an attribute.
The below xsl works fine if I do not bring in the "other_location_postal_code" field, which is commented here.
This is because there are multiple records if I bring in that field.
How can I have this xsl evaluate each record so it would write this record twice, once for the one "other location postal code" and the other?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:e="http://www.taleo.com/ws/tee800/2009/01" xmlns:fct="http://www.taleo.com/xsl_functions" exclude-result-prefixes="e fct">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:param name="OUTBOUND_FOLDER"/>
<xsl:template match="/">
<source>
<xsl:apply-templates select="//e:Requisition"/>
</source>
</xsl:template>
<xsl:template match="e:Requisition">
<xsl:variable name="job_id" select="e:ContestNumber"/>
<xsl:variable name="other_location_postal_code" select="e:JobInformation/e:JobInformation/e:OtherLocations/e:Location/e:NetworkLocation/e:NetworkLocation/e:ZipCode"/>
<job>
<job_id>
<xsl:value-of select="concat('<','![CDATA[',$job_id,']]','>')"/>
</job_id>
<other_location_postal_code>
<xsl:value-of select="concat('![CDATA[',$other_location_postal_code,']]')"/>
</other_location_postal_code>
</job>
</xsl:template>
</xsl:stylesheet>
I want it to come out like so:
<?xml version="1.0" encoding="UTF-8"?>
<source>
<job>
<job_id><![CDATA[15000005]]></job_id>
<other_location_postal_code><![CDATA[77382]]></other_location_postal_code>
</job>
<job>
<job_id><![CDATA[15000005]]></job_id>
<other_location_postal_code><![CDATA[37567]]></other_location_postal_code>
</job>
</source>
The initial XML looks like so:
<?xml version="1.0" encoding="UTF-8"?>
-<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
-<soapenv:Body>
-<ns1:getDocumentByKeyResponse xmlns:ns1="http://www.taleo.com/ws/integration/toolkit/2005/07" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
-<Document xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07">
-<Attributes>
<Attribute name="count">1</Attribute>
<Attribute name="duration">0:00:00.088</Attribute>
<Attribute name="entity">Requisition</Attribute>
<Attribute name="mode">T-XML</Attribute>
<Attribute name="version">http://www.taleo.com/ws/tee800/2009/01</Attribute>
</Attributes>
-<Content>
-<ExportTXML xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:e="http://www.taleo.com/ws/tee800/2009/01">
-<e:Requisition>
<e:ContestNumber>15000005</e:ContestNumber>
-<e:JobInformation>
-<e:JobInformation>
-<e:OtherLocations>
-<e:Location>
<e:ZipCode>77002</e:ZipCode>
</e:Location>
-<e:Location>
<e:ZipCode>77050</e:ZipCode>
</e:Location>
</e:OtherLocations>
</e:JobInformation>
</e:JobInformation>
</e:Requisition>
</ExportTXML>
</Content>
</Document>
</ns1:getDocumentByKeyResponse>
</soapenv:Body>
</soapenv:Envelope>
The error message simply says that an argument to the concat function is a sequence of more than one node. So some of your variable selects two or more nodes and concat expects as single node for each of its arguments. You will need to decide what you want to output, either only the first node with $var[1] or the concatenation with string-join($var, ' ').
Note that you don't need all those attempts to output CDATA section, you can tell the XSLT processor the cdata-section-elements on the xsl:output direction.
As you have now posted more details here is one suggestion to map each ZipCode element to job element:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:e="http://www.taleo.com/ws/tee800/2009/01"
exclude-result-prefixes="e">
<xsl:output indent="yes" cdata-section-elements="job_id other_location_postal_code"/>
<xsl:template match="/">
<source>
<xsl:apply-templates select="//e:OtherLocations/e:Location/e:ZipCode"/>
</source>
</xsl:template>
<xsl:template match="e:ZipCode">
<xsl:apply-templates select="ancestor::e:Requisition">
<xsl:with-param name="zc" select="current()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="e:Requisition">
<xsl:param name="zc"/>
<job>
<job_id><xsl:value-of select="e:ContestNumber"/></job_id>
<other_location_postal_code><xsl:value-of select="$zc"/></other_location_postal_code>
</job>
</xsl:template>
</xsl:stylesheet>
I have source xml looking like this :
<Data>
<ActionPlaces>
<ActionPlace>
<ActionPlaceID>74</ActionPlaceID>
<PlaceName>Theatre Of Classic</PlaceName>
</ActionPlace>
</ActionPlaces>
<Actions>
<CommonAction Id="2075" Name="King">
<Action>
<ActionID>4706</ActionID>
<ActionPlaceID>74</ActionPlaceID>
</Action>
</CommonAction>
</Actions>
</Data>
Which is to transform to this:
<category name="King">
<name>King</name>
<parent name="Theatre Of Classic" />
</category>
I want to use variable :
<xsl:template match="ActionPlaces">
<xsl:variable name="id" select="/ActionPlace/ActionPlaceID"/>
<xsl:template match="CommonAction" >
<category name="<xsl:value-of select="#name"/> >
<name><xsl:value-of select="#name"/></name>
<parent <xsl:if test="/Action/ActionPlaceID = $id">
name=/Action/ActionPlaceID/> <- how to get name of theatre here?
</xsl:template>
Can variable store not only id but name also? And how to get it? What is the most common approach to handle this ?
Here's one option using XSL keys (as #michael-hor257k suggested):
Input
<Root>
<ActionPlaces>
<ActionPlace>
<ActionPlaceID>74</ActionPlaceID>
<PlaceName>Theatre Of Classic</PlaceName>
</ActionPlace>
</ActionPlaces>
<Actions>
<CommonAction Id="2075" Name="King">
<Action>
<ActionID>4706</ActionID>
<ActionPlaceID>74</ActionPlaceID>
</Action>
</CommonAction>
</Actions>
</Root>
Stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<!-- Collect all <ActionPlace> elements into an XSL key -->
<xsl:key name="ActionPlaceById" match="ActionPlace" use="ActionPlaceID"/>
<xsl:template match="/">
<xsl:apply-templates select="Root/Actions/CommonAction"/>
</xsl:template>
<xsl:template match="CommonAction">
<category name="{#Name}">
<name>
<xsl:value-of select="#Name"/>
</name>
<!--
Using the ActionPlaceById key we created earlier, fetch the <ActionPlace>
element that has an <ActionPlaceID> child that has the same value as the
<ActionPlaceID> descendant of the current <CommonAction> element.
-->
<parent name="{key('ActionPlaceById', Action/ActionPlaceID)/PlaceName}"/>
</category>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="utf-8"?>
<category name="King">
<name>King</name>
<parent name="Theatre Of Classic"/>
</category>
I have a xml document which looks like
<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<AccountServer e-dtype="int">3</AccountServer>
</Header>
<Response>
<ResponseList e-dtype="list">
<Response>
<RequestId e-dtype="string">ServiceOrderGetRef</RequestId>
<RequestObjName e-dtype="string">ServiceOrder</RequestObjName>
<ServiceOrder>
<CreateDt e-dtype="dateTime">2014-03-01 00:00:00</CreateDt>
<CreateWho e-dtype="string">vc</CreateWho>
<WorkflowStartDt e-dtype="dateTime">2014-04-01 00:00:00</WorkflowStartDt>
</ServiceOrder>
</Response>
<Response>
<ComponentList e-dtype="list"/>
<Count e-dtype="int">0</Count>
<RequestId e-dtype="string">ComponentFindRef</RequestId>
<RequestObjName e-dtype="string">Component</RequestObjName>
<TotalCount e-dtype="int">0</TotalCount>
</Response>
<Response>
<Count e-dtype="int">0</Count>
<CustomerContractList e-dtype="list"/>
<RequestId e-dtype="string">CustomerContractRef</RequestId>
<RequestObjName e-dtype="string">CustomerContract</RequestObjName>
<TotalCount e-dtype="int">0</TotalCount>
</Response>
<Response>
<Count e-dtype="int">0</Count>
<ProductList e-dtype="list"/>
<RequestId e-dtype="string">ProductRef</RequestId>
<RequestObjName e-dtype="string">Product</RequestObjName>
<TotalCount e-dtype="int">0</TotalCount>
</Response>
<Response>
<Count e-dtype="int">0</Count>
<NrcList e-dtype="list"/>
<RequestId e-dtype="string">NrcFindRef</RequestId>
<RequestObjName e-dtype="string">Nrc</RequestObjName>
<TotalCount e-dtype="int">0</TotalCount>
</Response>
</ResponseList>
</Response>
</Request>
I am using copy-of function to copy node ServiceOrder within another xml document
I want to modify text of node WorkFlowStartDt and CreateDt and then do a copy-of. How can I do this?
My copied serviceorder node should look like this after modifing text. Below is the result xml
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess>
<Fetch e-dtype="boolean">true</Fetch>
<Order>
<AccountInternalId e-dtype="int">12345</AccountInternalId>
<Key>
<OrderId e-dtype="numeric">12345678</OrderId>
</Key>
</Order>
<ServiceOrderList e-dtype="list">
<ServiceOrder xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance">
<CreateDt e-dtype="dateTime">2014-03-02 00:00:00</CreateDt>
<CreateWho e-dtype="string">vc</CreateWho>
<WorkflowStartDt e-dtype="dateTime">2014-05-01 00:00:00</WorkflowStartDt>
</ServiceOrder>
</ServiceOrderList>
</LogicalServiceOrderPreProcess>
</LogicalServiceOrder>
</RequestList>
</CustomerUdtRequest>
</Request>
Below is my xslt processor file
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess> <Order> <AccountInternalId e-dtype="int">
<xsl:value-of
select="/Request/Response/ResponseList/Response/ServiceOrder/AccountInternalId"/>
</AccountInternalId> <Key> <OrderId e-dtype="numeric"> <xsl:value-of select="/Request/Response/ResponseList/Response/ServiceOrder/OrderId"/>
</OrderId>
</Key>
</Order>
<ServiceOrderList e-dtype="list">
<xsl:copy-of select="/Request/Response/ResponseList/Response/ServiceOrder"/>
</ServiceOrderList>
</LogicalServiceOrderPreProcess>
</LogicalServiceOrder>
</xsl:if>
</RequestList>
</CustomerUdtRequest>
</Request>
</xsl:template>
</xsl:stylesheet>
I want to modify text of node WorkFlowStartDt and CreateDt and then do
a copy-of.
That would be an unnecessary complication. You can modify nodes while you add them to the output tree.
Since you essentially want to copy everything "as is" except two nodes, it would be best to start with an identity transform template, then add an "exception" template for the two specific nodes that need modifying (one template for both, since the modification is identical).:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transformation -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CreateDt | WorkFlowStartDt">
<xsl:copy>
<xsl:value-of select="concat('**', ., '**')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Added:
In response to the edited question, try this stylesheet:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="fst"
exclude-result-prefixes="ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess>
<Order>
<AccountInternalId e-dtype="int">
<!-- THIS DOESN'T POINT TO ANY EXISTING NODE!! -->
<xsl:value-of select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder/ns:AccountInternalId"/>
</AccountInternalId>
<Key>
<OrderId e-dtype="numeric">
<!-- THIS DOESN'T POINT TO ANY EXISTING NODE!! -->
<xsl:value-of select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder/ns:OrderId"/>
</OrderId>
</Key>
</Order>
<xsl:apply-templates select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder"/>
</LogicalServiceOrderPreProcess>
</LogicalServiceOrder>
</RequestList>
</CustomerUdtRequest>
</Request>
</xsl:template>
<xsl:template match="ns:ServiceOrder">
<xsl:copy>
<xsl:copy-of select="ns:CreateWho"/>
<xsl:apply-templates select="ns:CreateDt | ns:WorkflowStartDt"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns:CreateDt | ns:WorkflowStartDt">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:value-of select="concat('**', ., '**')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to your input (minus the illegal opening comment), the following result is obtained:
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess>
<Order>
<AccountInternalId e-dtype="int"/>
<Key>
<OrderId e-dtype="numeric"/>
</Key>
</Order>
<ServiceOrder xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CreateWho e-dtype="string">vc</CreateWho>
<CreateDt e-dtype="dateTime">**2014-03-01 00:00:00**</CreateDt>
<WorkflowStartDt e-dtype="dateTime">**2014-04-01 00:00:00**</WorkflowStartDt>
</ServiceOrder>
</LogicalServiceOrderPreProcess>
</LogicalServiceOrder>
</RequestList>
</CustomerUdtRequest>
</Request>