0

I am working on an issue where I need to add an attribute to an element under certain conditions. Here is the XML that I have. When an AdditionalItem element has a non-empty Value element, I need to add an attribute called action as such:

    <AdditionalItems>
        **<AdditionalItem>**
            <Keys>
                <Key>Intake Source</Key>
            </Keys>
            <IdentifierDisplay>Intake Source</IdentifierDisplay>
            <DataType>
                <type>Enumeration</type>
                <enumeration>
                    <String>311</String>
                    <String>NIS Inspector</String>
                    <String>Other CCD Agency</String>
                </enumeration>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <Enumerations>
                    <Enumeration>
                        <Keys>
                            <Key>311</Key>
                        </Keys>
                        <IdentifierDisplay>311</IdentifierDisplay>
                    </Enumeration>
                    <Enumeration>
                        <Keys>
                            <Key>NIS Inspector</Key>
                        </Keys>
                        <IdentifierDisplay>NIS Inspector</IdentifierDisplay>
                    </Enumeration>
                    <Enumeration>
                        <Keys>
                            <Key>Other CCD Agency</Key>
                        </Keys>
                        <IdentifierDisplay>Other CCD Agency</IdentifierDisplay>
                    </Enumeration>
                </Enumerations>
                <inputRequired>false</inputRequired>
                <fieldType>Enumeration</fieldType>
            </DataType>
            <Name>Intake Source</Name>
            **<Value>311</Value>**
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Other CCD Agency</Key>
            </Keys>
            <IdentifierDisplay>Other CCD Agency</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>Other CCD Agency</Name>
            <Value/>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>311 Agent</Key>
            </Keys>
            <IdentifierDisplay>311 Agent</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>311 Agent</Name>
            <Value/>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Case Number</Key>
            </Keys>
            <IdentifierDisplay>Case Number</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>Case Number</Name>
            <Value/>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Case Created Date</Key>
            </Keys>
            <IdentifierDisplay>Case Created Date</IdentifierDisplay>
            <DataType>
                <type>Date</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Date</fieldType>
            </DataType>
            <Name>Case Created Date</Name>
            <Value/>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Complaintant Name:</Key>
            </Keys>
            <IdentifierDisplay>Complaintant Name:</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>Complaintant Name:</Name>
            <Value>Fred Fredderson</Value>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Phone Number:</Key>
            </Keys>
            <IdentifierDisplay>Phone Number:</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>Phone Number:</Name>
            <Value>3033333333</Value>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Email</Key>
            </Keys>
            <IdentifierDisplay>Email</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>Email</Name>
            <Value>[email protected]</Value>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Council District:</Key>
            </Keys>
            <IdentifierDisplay>Council District:</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>Council District:</Name>
            <Value/>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Inspector Distict:</Key>
            </Keys>
            <IdentifierDisplay>Inspector Distict:</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>Inspector Distict:</Name>
            <Value/>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
        <AdditionalItem>
            <Keys>
                <Key>Permit Number</Key>
            </Keys>
            <IdentifierDisplay>Permit Number</IdentifierDisplay>
            <DataType>
                <type>String</type>
                <inputRange>
                    <maxValue>0.0</maxValue>
                </inputRange>
                <inputRequired>false</inputRequired>
                <fieldType>Text</fieldType>
            </DataType>
            <Name>Permit Number</Name>
            <Value/>
            <security>F</security>
            <drillDown>false</drillDown>
        </AdditionalItem>
    </AdditionalItems>

My first thought was to do a for-each on //AdditionalItem, then check to see if the length of the Value element was > 0. If so, add the action attribute. Does that seem like a reasonable approach? Something similar to this:

<xsl:for-each select="/ns2:UpdateCAP/ns2:AdditionalInformation//AdditionalItem">
    <xsl:if test="string-length(Value) > 0">
      <!-- somehow add the attribute -->
    </xsl:if>
</xsl:for-each>
1
  • Oh - the attribute needs to be: <AdditionalItem action="Add"> Commented Sep 28, 2015 at 22:18

1 Answer 1

1
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

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

    <xsl:template match="AdditionalItem[Value[text()]]">
        <xsl:copy>
            <xsl:attribute name="action">Add</xsl:attribute>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

We can not change an existing document xml using xslt. We need to copy all the elements, adding an attribute to the desired location.
So, we copy all nodes and attributes using the first template and copy AdditionalItem node that contains a non-empty inner Value node, adding an attribute using second template.

AdditionalItem matches node with name AdditionalItem.
AdditionalItem[Value] matches node with name AdditionalItem and inner node with name Value that has any content (may be empty).
AdditionalItem[Value[text()]] matches node with name AdditionalItem and inner node with name Value that has some content (non-empty).

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

3 Comments

Thanks so much Alexander! It works like a charm. Can you explain what each of the parts are doing?
@MichaelLove - Update. Sorry, my English is poor.
That was an excellent description. Thanks so much for your help.

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.