0

Input XML Below:-

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Change xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Area>
            <Sender>
                <LogicalId>tyhu</LogicalId>
            </Sender>
            <CreationDateTime>2021-04-29T14:33:13Z</CreationDateTime>
            <Id1>
                <Id>163067354</Id>
            </Id1>
        </Area>
        <Data>
            <Prob>
                <DateTime>2021-04-29T14:33:13Z</DateTime>
            </Prob>
        </Data>
    </Change>

I need to add two elements Id2 and Id3 after Id1.

Desired Output:-

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Change xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Area>
            <Sender>
                <LogicalId>tyhu</LogicalId>
            </Sender>
            <CreationDateTime>2022-04-29T14:33:13Z</CreationDateTime>
            <Id1>
                <Id>6654</Id>
            </Id1>
            <Id2>C1</Id2>
            <Id3>29</Id3>
        </Area>
        <Data>
            <Prob>
                <DateTime>2022-04-29T14:33:13Z</DateTime>
            </Prob>
        </Data>
    </Change>

I tried below xslt but no luck:-

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output indent="yes"/>
        <xsl:mode on-no-match="shallow-copy"/>
        <xsl:template match="Id1">
            <Id2>C1</Id2>
            <Id3>29</Id3>
            <xsl:next-match/>
        </xsl:template>
    </xsl:stylesheet>

Please let me know if it is possible to do through xslt. Appreciate your help!

3
  • Your stylesheet declares version="1.0" yet uses an instruction that requires XSLT 3.0. Which XSLT processor will you be using? Also "no luck" is not a good description of a problem. Commented May 31, 2021 at 14:58
  • Hi @michael, Sorry, getting below error:- "Unable to generate the XML document using the provided XML/XSL input. Errors were reported during stylesheet compilation" Commented May 31, 2021 at 18:12
  • I asked which processor you are using. If you don't know, see here how to find out: stackoverflow.com/a/25245033/3016153 Commented May 31, 2021 at 18:39

1 Answer 1

1

Simply use this template:

<xsl:template match="Id1">
  <xsl:next-match />
  <Id2>C1</Id2>
  <Id3>29</Id3>
</xsl:template>

To complete the task with XSLT-1.0, you would have to use the identity template and replace the <xsl:next-match /> by <xsl:copy-of select="." />, but with XSLT-3.0, you can use the <xsl:mode on-no-match="shallow-copy"/>, as you did in your example.

The result is:

<?xml version="1.0" encoding="UTF-8"?>
<Change xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Area>
      <Sender>
         <LogicalId>tyhu</LogicalId>
      </Sender>
      <CreationDateTime>2021-04-29T14:33:13Z</CreationDateTime>
      <Id1>
         <Id>163067354</Id>
      </Id1>
      <Id2>C1</Id2>
      <Id3>29</Id3>
   </Area>
   <Data>
      <Prob>
         <DateTime>2021-04-29T14:33:13Z</DateTime>
      </Prob>
   </Data>
</Change>
Sign up to request clarification or add additional context in comments.

4 Comments

With XSLT 3.0, you can use xsl:mode ..., not "have to". And with a processor that supports XSLT 3.0, all that OP needs to do is move the <xsl:next-match/> instruction ahead of the two literal result elements.
@michael.hor257k: Thanks. That really improved the whole template.
Hi @zx485 I am getting spaces at the starting.
Not sure at which place you get spaces. One possibility to remove spaces from elements is adding <xsl:strip-space elements="*" /> at the stylesheet level.

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.