Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kevin Butler 2 posts 22 karma points
    Nov 17, 2011 @ 02:56
    Kevin Butler
    0

    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

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Nov 17, 2011 @ 08:45
    Chriztian Steinmeier
    1

    Hi Kevin,

    Templates to the rescue - start with an "Identity Transform" template:

    <!-- Identity transform -->
    <xsl:template match="* | text()">
        <xsl:copy>
            <xsl:copy-of select="@*" />
            <xsl:apply-templates select="* | text()" />
        </xsl:copy>
    </xsl: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":

    <!-- 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. 

    /Chriztian

  • Kevin Butler 2 posts 22 karma points
    Nov 17, 2011 @ 13:56
    Kevin Butler
    0

    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

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Nov 18, 2011 @ 16:16
    Chriztian Steinmeier
    0

    Hi Kevin,

    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>

    /Chriztian

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Nov 18, 2011 @ 16:20
    Chriztian Steinmeier
    0

    - 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:

    <!ENTITY CURRENTLABEL "SOMEOTHERVALUE">

    /Chriztian 

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies