0

Have an XML code as follows (partially):

    <ItemMaster>
            <Classification>
                <Codes>
                    <Code listID="Item Types" sequence="1">Cost</Code>
                    <Code listID="Item Groups" sequence="2">4ET</Code>
                    <Code listID="MRO Classes" sequence="3">*</Code>
                </Codes>
            </Classification>   
    </ItemMaster>

I want to change the Item Types from "Cost" to "Purchased". So it will look like this :

<ItemMaster>
            <Classification>
                <Codes>
                    <Code listID="Item Types" sequence="1">Purchased</Code>
                    <Code listID="Item Groups" sequence="2">4ET</Code>
                    <Code listID="MRO Classes" sequence="3">*</Code>
                </Codes>
            </Classification>   
    </ItemMaster>

I want to use the "choose" function, because I need to change the value based on a condition, such as IF the Item Groups is 4ET, then change the Item Types to change to Purchased

usually I can match the element. But now, since the data is inside an attribute, I don't know how to match and change it

UPDATE: I tried these codes

</xsl:template>
    <xsl:template
        match="//my:ItemMaster/my:Classification/my:Codes/my:Code[1]">
    <xsl:variable name="ItemGroup" 
        select="//my:ItemMaster/my:Classification/my:Codes/my:Code[@listID='Item Groups']"/>
    <xsl:choose>
        <xsl:when test="($ItemGroup='4ET')">
                <xsl:element name="Code[1]">
                    <xsl:value-of select="'Purchased'" />
                </xsl:element>              
        </xsl:when>
        <xsl:otherwise>
                <xsl:element name="Code[1]">
                    <xsl:value-of select="'Cost'" />
                </xsl:element>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

And then I got this result :

<Classification>
                <Codes>
                    Stock
                    <Code listID="Item Groups" sequence="2">4ET</Code>
                    <Code listID="MRO Classes" sequence="3">*</Code>
                </Codes>
            </Classification>

So, it is still not like what I needed.

Please help me :( Thank you for the kind help.

2 Answers 2

2

If all you want to do is change 'Cost' to 'Purchased', you could do an identity transform, which means applying a recursive template that simply copies the node, on all nodes, and have a separate template for all text nodes of which the value is 'Cost'.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

    <xsl:template match="node() | @*">
        <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="text()[. = 'Cost']">
        <xsl:text>Purchase</xsl:text>
    </xsl:template>

</xsl:stylesheet>

Updated code based on response:

<?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" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

    <xsl:template match="node() | @*">
        <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Code[@listID='Item Types']">
        <xsl:variable name="itemGroup" select="../Code[@listID='Item Groups']"/>
        <Code listID="Item Types" sequence="{@sequence}">
            <xsl:choose>
                <xsl:when test="$itemGroup = '4ET'">
                    <xsl:text>Purchased</xsl:text>
                </xsl:when>
                <xsl:when test="$itemGroup = 'something else'">
                    <xsl:text>...</xsl:text>
                </xsl:when>
            </xsl:choose>
            </Code>
    </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

7 Comments

Actually I need to use the Choose like the example on my question. But I will try this out first. Thanks !
Hi wasmachien, I have tried it, it is not working, because I need to use "choose" function. The case is to change the value if a condition was met. Could you incorporate the answer by using "Choose"?
@ArnoldyMoniaga something like this?
@washmachien I have tried, but it is still not right. The result is only a word "Purchased" in one line. It is the same result like I stated above. I need the transformation exactly like <Code listID="Item Types" sequence="1">Purchased</Code> . Right now, it is only one text : "Purchased"
@ArnoldyMoniaga When you run the second code snippet on your supplied input XML, the result is <Code listID="Item Types" sequence="1">Purchased</Code>. Are you running the correct code?
|
0

Check this out please;

<xsl:template match="/">
    <ItemMaster>
        <Classification>
            <Codes>
                <Code listID="Item Types" sequence="1">
                    <xsl:value-of select="'Purchase'"/>
                </Code>
                <Code listID="Item Groups" sequence="2">
                    <xsl:apply-templates select="//Code[2]"/>
                </Code>
                <Code listID="MRO Classes" sequence="3">
                    <xsl:apply-templates select="//Code[3]"/>
                </Code>
            </Codes>
        </Classification>
    </ItemMaster>
</xsl:template>

When you apply, it will change the value of your node

6 Comments

Hi Sojimanatsu, I have tried it, but it resulted like this : <Classification> <Codes> Stock <Code listID="Item Groups" sequence="2">4ET</Code> <Code listID="MRO Classes" sequence="3">*</Code> </Codes> </Classification>
I just wanted to give you an idea of Xpath selection, this could be done by many ways, the ugly solution would be in edited code, check it out.
OK. Thank you so much. The Xpath works. I got another way to select the data. But when it tries to write the result, something is still not right.
If it is working, please accept the answer, if not tell me the problem and lets fix it
Hi Sojimanatsu, could you incorporate your solution according to my case ? in my case, I need to change the Item type from "Cost" to "Purchase" IF the item group is "4ET". I hope you are okay with this. Thank you :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.