0

I have the following input xml:

 <?xml version="1.0" encoding="UTF-8"?>
    <GroupMainRequest>
       <GroupMainResults>
          <GroupNumber>GLEN01</GroupNumber>
          <divisions>
             <userProvidedEffDate>12/31/1998</userProvidedEffDate>
             <division>
                <GroupNumber>GLEN010001</GroupNumber>
                <GroupEffDt>01/01/2000</GroupEffDt>
                <GroupExpDt />
             </division>
             <division>
                <GroupNumber>GLEN010002</GroupNumber>
                <GroupEffDt>01/01/2000</GroupEffDt>
                <GroupExpDt />
             </division>
             <division>
                <GroupNumber>GLEN010003</GroupNumber>
                <GroupEffDt>01/01/2000</GroupEffDt>
                <GroupExpDt>12/31/2001</GroupExpDt>
             </division>
          </divisions>
       </GroupMainResults>
    </GroupMainRequest>

I need to update the value of the node <GroupExpDt> of each <division> under <divisions> to 12/31/9999 whenever the node value is empty or null( I am using XSLT 1.0).

The final xml output should be as below:

<?xml version="1.0" encoding="UTF-8"?>
    <GroupMainRequest>
       <GroupMainResults>
          <GroupNumber>GLEN01</GroupNumber>
          <divisions>
             <userProvidedEffDate>12/31/1998</userProvidedEffDate>
             <division>
                <GroupNumber>GLEN010001</GroupNumber>
                <GroupEffDt>01/01/2000</GroupEffDt>
                <GroupExpDt>12/31/9999</GroupExpDt>
             </division>
             <division>
                <GroupNumber>GLEN010002</GroupNumber>
                <GroupEffDt>01/01/2000</GroupEffDt>
                <GroupExpDt>12/31/9999</GroupExpDt>
             </division>
             <division>
                <GroupNumber>GLEN010003</GroupNumber>
                <GroupEffDt>01/01/2000</GroupEffDt>
                <GroupExpDt>12/31/2001</GroupExpDt>
             </division>
          </divisions>
       </GroupMainResults>
    </GroupMainRequest>

Any help is much appreciated.

1
  • "I need" is not a question. Where exactly are you stuck with this? Commented Jun 3, 2019 at 7:40

1 Answer 1

1

Related to another question: Copy all nodes with special cases

So upvote his answer too please ;)

Transformed his code a bit:

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

<!-- Identity template, provides default behavior that copies all content into the output -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- More specific template for GroupExpDt that provides custom behavior -->
<!-- Documentation below code block -->
<xsl:template match="GroupExpDt[string-length(normalize-space(text())) = 0]">  
    <GroupExpDt>
        <xsl:text>12/31/9999</xsl:text>
    </GroupExpDt>
</xsl:template>
</xsl:stylesheet>

normalize-space() documentation

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

1 Comment

You could slightly simplify the template match to <xsl:template match="GroupExpDt[not(normalize-space())]">.

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.