Add a new sibling node after matching attribute value

Viewed 39

I need to add a node after a template match. The template match is on an attribute value, based on a parameter. I have been successful at adding the node as a child of the matched attribute. However, I need the added node to be a sibling node, not a child node. Is there a way to add the note as a sibling, not as a child of the matched attribute?

This is my xml file:

 <WORK SRCDBID="DBIDxx" DSTDBID="SERVER" WORKTYPE="DELTA" SETNUMBER="1">
 <TXID SRCDBID="DBIDxx" CPDATE="2021200932651" TYPE="0">
 <OP ACTION="I" TBL="RTD-WORKORDER">
 <COLS>
 <COL NAME="WoNum" VAL="303105525"/>
 <COL NAME="NumWoLin" VAL="1"/>
 <COL NAME="LinNum" VAL="1"/>
 <COL NAME="RtrdTag" VAL="527395802"/>
 </COLS>
 </OP>
 </TXID>
 </WORK>

My failing result:

<WORK SRCDBID="DBIDxx" DSTDBID="SERVER" WORKTYPE="DELTA" SETNUMBER="1">
<TXID SRCDBID="DBIDxx" CPDATE="2021200932651" TYPE="0">
<OP ACTION="I" TBL="RTD-WORKORDER">
<COLS>
<COL NAME="WoNum" VAL="303105525"/>
<COL NAME="NumWoLin" VAL="1"/>
<COL NAME="LinNum" VAL="1"/>
<COL NAME="RtrdTag" VAL="527395802">
    <COL NAME="DuplicateTag" VAL="0303105525|31"/>
</COL>
</COLS>
</OP>
</TXID>
</WORK>

My desired result:

<WORK SRCDBID="DBIDxx" DSTDBID="SERVER" WORKTYPE="DELTA" SETNUMBER="1">
<TXID SRCDBID="DBIDxx" CPDATE="2021200932651" TYPE="0">
<OP ACTION="I" TBL="RTD-WORKORDER">
<COLS>
<COL NAME="WoNum" VAL="303105525"/>
<COL NAME="NumWoLin" VAL="1"/>
<COL NAME="LinNum" VAL="1"/>
<COL NAME="RtrdTag" VAL="527395802"/>
<COL NAME="DuplicateTag" VAL="031123123|31"/>
</COLS>
</OP>
</TXID>
</WORK>

My xsl: Parameter values are: rtdTag = "527395802" rtdDupTag = "0303105525|31"

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" media-type="text/xml" method="xml" />

  <xsl:param name="rtdTag" />
  <xsl:param name="rtdDupTag" />

  <!-- This is the default template that copied everything -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- This is the "override" template for specific elements that match the passed in tag. -->
  <xsl:template match="@*[. = $rtdTag]">
    <!-- Copy the element everything inside it -->
    <xsl:copy>
      <xsl:copy-of select="node()"/>
    </xsl:copy>

    <!-- Add new node  -->
      <xsl:element name="COL">
        <xsl:attribute name="NAME">
          <xsl:text>DuplicateTag</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="VAL">
          <xsl:value-of select="$rtdDupTag"/>
        </xsl:attribute>
        <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>
</xsl:stylesheet>
1 Answers

Change your template to match on the element that has that attribute, and then add your new element after copying that element.

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" media-type="text/xml" method="xml" />
    
    <xsl:param name="rtdTag" />
    <xsl:param name="rtdDupTag" />
    
    <!-- This is the default template that copied everything -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- This is the "override" template for specific elements that match the passed in tag. -->
    <xsl:template match="*[@* = $rtdTag]">
        <!-- Copy the element everything inside it -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
        
        <!-- Add new node  -->
        <xsl:element name="COL">
            <xsl:attribute name="NAME">
                <xsl:text>DuplicateTag</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="VAL">
                <xsl:value-of select="$rtdDupTag"/>
            </xsl:attribute>
            
        </xsl:element>

    </xsl:template>
</xsl:stylesheet>

And if you know the names of the element and attribute statically, I prefer element and attribute literals instead of using xsl:element and xsl:attribute:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" media-type="text/xml" method="xml" />
    
    <xsl:param name="rtdTag" />
    <xsl:param name="rtdDupTag" />
    
    <!-- This is the default template that copied everything -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- This is the "override" template for specific elements that match the passed in tag. -->
    <xsl:template match="*[@* = $rtdTag]">
        <!-- Copy the element everything inside it -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
        
        <!-- Add new node  -->
        <COL NAME="DuplicateTag" VAL="{$rtdDupTag}"/>
    </xsl:template>
    
</xsl:stylesheet>
Related