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!

John
  • 105
  • 9
  • 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. – michael.hor257k May 31 '21 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" – John May 31 '21 at 18:12
  • I asked which processor you are using. If you don't know, see here how to find out: https://stackoverflow.com/a/25245033/3016153 – michael.hor257k May 31 '21 at 18:39

1 Answers1

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>
zx485
  • 28,498
  • 28
  • 50
  • 59
  • 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 `` instruction ahead of the two literal result elements. – michael.hor257k May 31 '21 at 15:07
  • @michael.hor257k: Thanks. That really improved the whole template. – zx485 May 31 '21 at 15:18
  • Hi @zx485 I am getting spaces at the starting. – John May 31 '21 at 18:10
  • Not sure at which place you get spaces. One possibility to remove spaces from elements is adding `` at the stylesheet level. – zx485 Jun 01 '21 at 01:53