<orderView> <partyReference href="#NEWFIELD> <comments> <comment>Info about order</comment> <comment>More info about order</comment> <comment/> </comments> </orderView> <orderViews> <orderView> <strategy>??</strategy> </orderView> </order> </orders> </MxML>
The transformation will be based on a few outcomes...
Basically if the <strategy> tag is equal to A or B I would like every instance of NEW_FIELD to Equal 'A' or if the <strategy> take is anything wlse i would like every instance of NEW_FIELD to Equal 'B
- this should copy the whole file verbatim; then add a template for the nodes you want to change, e.g. if you want to add a <coolStuff/> node to all <mainEvent>s that have the action "insertion":
<!-- Catch mainEvent nodes with an "insertion" action -->
<xsl:template match="mainEvent[action = 'insertion']">
<!-- Add new node -->
<coolStuff />
<!-- Continue processing -->
<xsl:apply-templates />
</xsl:template>
To actually help you with the problem at hand, just show us how the resulting XML should look, compared to the original - it's hard to discern from the description you gav, maybe just because we really don't have a clue about the format of the original data.
Firstly, thanks for the reply. Reading back over this it wasn't very clear. At the start i will declare two variables
V1 = 'NEWLABEL1'
V2 = 'NEWLABLEL2'
There are two scenarios
1. The value of the strategy tag starts with ABCD ....(and cant have any characters after this). If this is the case I want to replace the value 'CURRENTLABEL' , which can occur in many places, with the value of V1 above
otherwise
2. THe value of the??doesnt equal ABCD* and therefore gets assigned the value of V2
(to throw a spanner in the works sometimes the value i want to replace has a # in front of it.)
Try this, and see if it does the job - ask away if you want to know the inner mechanics of it :-)
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE xsl:stylesheet [
<!-- This needs to be an Entity because you can not use a variable in a template match -->
<!ENTITY CURRENTLABEL "CURRENTLABEL">
]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<!-- We all know these are not the *actual* values :-) -->
<xsl:variable name="prefix" select="'ABCD'" />
<xsl:variable name="V1" select="'NEWLABEL1'" />
<xsl:variable name="V2" select="'NEWLABEL2'" />
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="@id[. = '&CURRENTLABEL;'] | @href[. = '#&CURRENTLABEL;']">
<!-- Add a hash (#) if we are creating an href attribute -->
<xsl:variable name="hash" select="substring('#', not(name() = 'href') * 1 + 1, 1)" />
<xsl:attribute name="{name()}">
<xsl:value-of select="$hash" />
<xsl:choose>
<!-- May have to modify the test here for correct scope in finding a <strategy> element... -->
<xsl:when test="starts-with(ancestor::order[1]//strategy, $prefix)">
<xsl:value-of select="$V1" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$V2" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
- Oh, and it may not be obvious if you (or a future reader) don't know about entites, but to look for another value than CURRENTLABEL, you only have to change the quoted value:
XSL Transformation based on some conditions
Hi
I have been asked to do a small project using xslt. I dont have a coding background and need some help/advice.
My XML file is something like this
<?xml version="1.0"?>
<MxML version="1-1">
<events>
<mainEvent>
<action>insertion</action>
<object href="123456789">
<objectNature type="string">new_order</objectNature>
</object>
</mainEvent>
</events>
<documentProperties type="documentProperties">
<producedBy>
<partyName type="string">MIDLAND CO</partyName>
</producedBy>
</documentProperties>
<orders type="orders">
<order id="order_123456" type="order">
<parties type="Parties">
<party id="MIDLAND_CO" type="Party">
<partyName type="string">MIDLAND CO</partyName>
<partyFullName type="string">MIDLAND CO</partyFullName>
<partyCode type="string">MIDLAND CO22</partyCode>
</party>
<party id="MIDLATH" type="Party">
<partyName type="string">NEW_FIELD</partyName>
</party>
<-- Skipping some irrelevant lines ->>
<orderView>
<partyReference href="#NEWFIELD>
<comments>
<comment>Info about order</comment>
<comment>More info about order</comment>
<comment/>
</comments>
</orderView>
<orderViews>
<orderView>
<strategy>??</strategy>
</orderView>
</order>
</orders>
</MxML>
The transformation will be based on a few outcomes...
Basically if the <strategy> tag is equal to A or B I would like every instance of NEW_FIELD to Equal 'A' or if the <strategy> take is anything wlse i would like every instance of NEW_FIELD to Equal 'B
Thanks in advance
' Kevin
Hi Kevin,
Templates to the rescue - start with an "Identity Transform" template:
- this should copy the whole file verbatim; then add a template for the nodes you want to change, e.g. if you want to add a <coolStuff/> node to all <mainEvent>s that have the action "insertion":
To actually help you with the problem at hand, just show us how the resulting XML should look, compared to the original - it's hard to discern from the description you gav, maybe just because we really don't have a clue about the format of the original data.
/Chriztian
Firstly, thanks for the reply. Reading back over this it wasn't very clear. At the start i will declare two variables
V1 = 'NEWLABEL1'
V2 = 'NEWLABLEL2'
There are two scenarios
1. The value of the strategy tag starts with ABCD ....(and cant have any characters after this). If this is the case I want to replace the value 'CURRENTLABEL' , which can occur in many places, with the value of V1 above
otherwise
2. THe value of the??doesnt equal ABCD* and therefore gets assigned the value of V2
(to throw a spanner in the works sometimes the value i want to replace has a # in front of it.)
< -- <?xml version="1.0"?>
<MxML version="1-1">
<events>
<mainEvent>
<action>insertion</action>
<object href="123456789">
<objectNature type="string">new_order</objectNature>
</object>
</mainEvent>
</events>
<documentProperties type="documentProperties">
<producedBy>
<partyName type="string">MIDLAND CO</partyName>
</producedBy>
</documentProperties>
<orders type="orders">
<order id="order_123456" type="order">
<parties type="Parties">
<party id="CURRENTLABEL" type="Party">
<partyName type="string">MIDLAND CO</partyName>
<partyFullName type="string">MIDLAND CO</partyFullName>
<partyCode type="string">MIDLAND CO22</partyCode>
</party>
<party id="CURRENTLABEL" type="Party">
<partyName type="string">NEW_FIELD</partyName>
</party>
<-- Skipping some irrelevant lines ->>
<orderView>
<partyReference href="#CURRENTLABEL>
<comments>
<comment>Info about order</comment>
<comment>More info about order</comment>
<comment/>
</comments>
</orderView>
<orderViews>
<orderView>
<strategy>??</strategy>
</orderView>
</order>
</orders>
</MxML> -->
Thanks in advance!
K
Hi Kevin,
Try this, and see if it does the job - ask away if you want to know the inner mechanics of it :-)
/Chriztian
- Oh, and it may not be obvious if you (or a future reader) don't know about entites, but to look for another value than CURRENTLABEL, you only have to change the quoted value:
/Chriztian
is working on a reply...