redsam
redsam

Reputation: 145

Create new nodes for the children and copy parent info using XSLT

I have a requirement, need to copy all of parent node info for each child reference nodes. can't figure out on my own. Any help will be highly appreciated. Here is the transformation code. Need to copy all the information from parent for each "Relationship_410".

 <xsl:template match="/ns0:Records" mode="pass2">
        <Records xmlns="http://www.tech.com/">
          <xsl:for-each select="ns0:Record">
            <xsl:for-each select="ns0:Relationship_397">
 <xsl:for-each select="ns0:Relationship_410">
              <Record>           
                <xsl:copy-of select="./node()[not(self::ns0:Relationship_410)]"></xsl:copy-of>
              </Record>
 </xsl:for-each>
            </xsl:for-each>
          </xsl:for-each>
        </Records>
      </xsl:template>

INPUT:

 <Records>
    <Record>
      <Tracking_ID>7</Tracking_ID>
      <Relationship_397>
        <Field_contentId>12099237</Field_contentId>       
        <Relationship_410>
          <Field_contentId>12102605</Field_contentId>          
          <Issue_Criticality>
            <Item>High</Item>
          </Issue_Criticality>         
        </Relationship_410>
            <Relationship_410>
          <Field_contentId>test -- 12102605</Field_contentId>         
          <Issue_Criticality>
            <Item>test --High</Item>
          </Issue_Criticality>          
        </Relationship_410>
        <Relationship_49>
          <Field_contentId>7358689</Field_contentId>
          <Tracking_ID>7358689</Tracking_ID>
        </Relationship_49>          
      </Relationship_397>
      <Relationship_124>
        <Field_contentId>5981551</Field_contentId>       
      </Relationship_124>
      <Relationship_124>
        <Field_contentId>5985378</Field_contentId>        
      </Relationship_124>
    </Record>
  </Records>

OUTPUT

 <Records>
  <Record>
    <Tracking_ID>7</Tracking_ID>
    <Relationship_397>
      <Field_contentId>12099237</Field_contentId>
      <Relationship_410>
        <Field_contentId>12102605</Field_contentId>
        <Issue_Criticality>
          <Item>High</Item>
        </Issue_Criticality>
      </Relationship_410>
      <Relationship_49>
        <Field_contentId>7358689</Field_contentId>
        <Tracking_ID>7358689</Tracking_ID>
      </Relationship_49>
    </Relationship_397>
    <Relationship_124>
      <Field_contentId>5981551</Field_contentId>
    </Relationship_124>
    <Relationship_124>
      <Field_contentId>5985378</Field_contentId>
    </Relationship_124>
  </Record>
  <Record>
    <Tracking_ID>7</Tracking_ID>
    <Relationship_397>
      <Field_contentId>12099237</Field_contentId>
      <Relationship_410>
        <Field_contentId>test -- 12102605</Field_contentId>
        <Issue_Criticality>
          <Item>test --High</Item>
        </Issue_Criticality>
      </Relationship_410>
      <Relationship_49>
        <Field_contentId>7358689</Field_contentId>
        <Tracking_ID>7358689</Tracking_ID>
      </Relationship_49>
    </Relationship_397>
    <Relationship_124>
      <Field_contentId>5981551</Field_contentId>
    </Relationship_124>
    <Relationship_124>
      <Field_contentId>5985378</Field_contentId>
    </Relationship_124>
  </Record>
</Records>

Upvotes: 0

Views: 96

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116957

AFAICT, the required output can be produced quite easily by:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/Records">
    <Records>
        <xsl:for-each select="//Relationship_410">
            <Record>
                <xsl:copy-of select="../../Tracking_ID"/>
                <Relationship_397>
                    <xsl:copy-of select="../../Relationship_397/Field_contentId"/>
                    <xsl:copy-of select="."/>
                    <xsl:copy-of select="../Relationship_49"/>
                </Relationship_397>
                <xsl:copy-of select="../../Relationship_124"/>
            </Record>
        </xsl:for-each>
    </Records>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

hr_117
hr_117

Reputation: 9627

You may try something like this:

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

  <xsl:template match="Relationship_410"> 
      <xsl:param name="r410" />
      <xsl:if test="$r410 = generate-id(.)" >
        <xsl:copy>
            <xsl:apply-templates  />
        </xsl:copy>
      </xsl:if>
  </xsl:template>

  <xsl:template match="/Records"> 
    <xsl:copy>
        <xsl:apply-templates select="//Relationship_410" mode="gen410" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Relationship_410" mode="gen410">
      <xsl:apply-templates select="ancestor::Record">
        <xsl:with-param name="r410" select="generate-id(.)" />
      </xsl:apply-templates>
  </xsl:template>

Upvotes: 1

Related Questions