1

I'm trying to use XSLT and replace xml element value by another element if second element value is not empty. In example below I need to replace Item tag value by ItemMaster-CustomChar10 tag value only if it exists

<WmsShippingResultOutSiEs>
    <RecordId>6</RecordId>
    <ShipmentTransactionId>146</ShipmentTransactionId>
    <OutboundOrder>ERIC_1</OutboundOrder>
    <WmsShippingResultLineOutSiEs>
        <Line>
            <RecordId>6</RecordId>
            <ShipmentLineSequence>1</ShipmentLineSequence>
            <Item>BMS9</Item>
            <ItemMaster-CustomChar10>BMS9ALIAS</ItemMaster-CustomChar10>
            <WmsShippingResultLineDetailOutSiEs>
                <Line>
                    <ShipmentLineSequence>1</ShipmentLineSequence>
                    <ShipmentLineDetailTransactionId>143</ShipmentLineDetailTransactionId>
                </Line>
            </WmsShippingResultLineDetailOutSiEs>
        </Line>
        <Line>
            <RecordId>6</RecordId>
            <ShipmentLineSequence>2</ShipmentLineSequence>
            <Item>BMS10</Item>
            <ItemMaster-CustomChar10/>
            <WmsShippingResultLineDetailOutSiEs>
                <Line>
                    <ShipmentLineSequence>1</ShipmentLineSequence>
                    <ShipmentLineDetailTransactionId>144</ShipmentLineDetailTransactionId>
                </Line>
                <Line>
                    <ShipmentLineSequence>2</ShipmentLineSequence>
                    <ShipmentLineDetailTransactionId>145</ShipmentLineDetailTransactionId>
                </Line>
            </WmsShippingResultLineDetailOutSiEs>
        </Line>
    </WmsShippingResultLineOutSiEs>
</WmsShippingResultOutSiEs>

I'm expecting to have a result like

<WmsShippingResultOutSiEs>
    <RecordId>6</RecordId>
    <ShipmentTransactionId>146</ShipmentTransactionId>
    <OutboundOrder>ERIC_1</OutboundOrder>
    <WmsShippingResultLineOutSiEs>
        <Line>
            <RecordId>6</RecordId>
            <ShipmentLineSequence>1</ShipmentLineSequence>
            <Item>BMS9ALIAS</Item>
            <ItemMaster-CustomChar10>BMS9ALIAS</ItemMaster-CustomChar10>
            <WmsShippingResultLineDetailOutSiEs>
                <Line>
                    <ShipmentLineSequence>1</ShipmentLineSequence>
                    <ShipmentLineDetailTransactionId>143</ShipmentLineDetailTransactionId>
                </Line>
            </WmsShippingResultLineDetailOutSiEs>
        </Line>
        <Line>
            <RecordId>6</RecordId>
            <ShipmentLineSequence>2</ShipmentLineSequence>
            <Item>BMS10</Item>
            <ItemMaster-CustomChar10/>
            <WmsShippingResultLineDetailOutSiEs>
                <Line>
                    <ShipmentLineSequence>1</ShipmentLineSequence>
                    <ShipmentLineDetailTransactionId>144</ShipmentLineDetailTransactionId>
                </Line>
                <Line>
                    <ShipmentLineSequence>2</ShipmentLineSequence>
                    <ShipmentLineDetailTransactionId>145</ShipmentLineDetailTransactionId>
                </Line>
            </WmsShippingResultLineDetailOutSiEs>
        </Line>
    </WmsShippingResultLineOutSiEs>
</WmsShippingResultOutSiEs>

All other elements need shouldn't be affected. Is it possible to do it recursivly?

My XSLT code below doesn't work

<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" omit-xml-declaration="no"/>
<!-- identity template -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:for-each select="//WmsShippingResultLineDetailOutSiEs/Lines">
    <xsl:variable name="host_item" select="ItemMaster-CustomChar10"/>
    <xsl:if test="ItemMaster-CustomChar10 !=''">
        <Item>
            <xsl:value-of select="$host_item"/>
        </Item>
    </xsl:if>
</xsl:for-each>

Thanks a lot

1 Answer 1

3

As with most XML to XML transformations, I would suggest to use the identity transformation as the starting point and then add a template for the element(s) you want to change:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

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

  <xsl:template match="Line[ItemMaster-CustomChar10[normalize-space()]]/Item">
      <xsl:copy>
          <xsl:value-of select="../ItemMaster-CustomChar10"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bEzknsJ

Sign up to request clarification or add additional context in comments.

Comments

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.