RadicalJuniper
RadicalJuniper

Reputation: 60

XSL - Replace node value from other structure but with the same name

I am trying to replace some nodes value. The idea is searching for the node with the same name in other structure and replace the value.

Here I am changing the values from Body/A with Rule/A and Body/C/D with Rule/D.

Something like this.

<Message>
<XMLNSC>
    <MaXML>
        <Rule>
            <A>RuleA</A>
            <D>RuleD</D>
            <E>RuleE</E>
        </Rule>
        <Body>
            <A>valA</A>
            <B>valB</B>
            <C>
                <D>valD</D>
            </C>
        </Body>
    </MaXML>
</XMLNSC>

To this.

<Message>
<XMLNSC>
    <MaXML>
        <Rule>
            <A>RuleA</A>
            <D>RuleD</D>
            <E>RuleE</E>
        </Rule>
        <Body>
            <A>RuleA</A>
            <B>valB</B>
            <C>
                <D>RuleD</D>
            </C>
        </Body>
    </MaXML>
</XMLNSC>

Currently I am looping through all children, searching for the match between nodes' names, but how should I keep the same structure and just change the value?

<?xml version="1.0" encoding="utf-8"?>

<xsl:output method="xml" indent="yes"/>

<!-- copy all -->
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<!-- match Body -->
<xsl:template match="Body">
    <Body>
        <!-- all children from body -->
        <xsl:for-each select="descendant::*">
            <xsl:variable name="maNode" select="."/>
            <xsl:variable name="nodeName" select="name()"/>
            <!-- all rules -->
            <xsl:for-each select="//Rule/*">
                <xsl:variable name="varRuleName" select="name()"/>  
                <xsl:choose>
                    <!-- match rule -->
                    <xsl:when test="$nodeName = $varRuleName">
                        <element name="match"/>
                        <xsl:copy>
                            <xsl:apply-templates select="@* | node()"/>
                        </xsl:copy>
                    </xsl:when>
                    <xsl:otherwise>
                        <element name="unmatch"/>
                        <xsl:copy>
                            <xsl:apply-templates select="@* | node()"/>
                        </xsl:copy>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:for-each>
    </Body>
</xsl:template>

Upvotes: 2

Views: 1176

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

The approach with the identity template

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

is fine, now to use that you just need to add templates for those elements you want to change e.g.

  <xsl:template match="Body/A">
      <xsl:copy>
          <xsl:value-of select="ancestor::MaXML/Rule/A"/>
      </xsl:copy>
  </xsl:template>

https://xsltfiddle.liberty-development.net/pPqsHTK

It should be clear I hope how to add a third template matching Body/C/D and selecting the value from the corresponding Rule as needed.

If you need a generic approach then in XSLT 2 or 3 that is easy with a key and a key function call in the subtree:

  <xsl:key name="ref" match="MaXML/Rule/*" use="node-name()"/>

  <xsl:template match="Body//*[key('ref', node-name(), ancestor::MaXML)]">
      <xsl:copy>
          <xsl:value-of select="key('ref', node-name(), ancestor::MaXML)"/>
      </xsl:copy>
  </xsl:template>

Full XSLT 3 at https://xsltfiddle.liberty-development.net/pPqsHTK/3 is then

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

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:key name="ref" match="MaXML/Rule/*" use="node-name()"/>

  <xsl:template match="Body//*[key('ref', node-name(), ancestor::MaXML)]">
      <xsl:copy>
          <xsl:value-of select="key('ref', node-name(), ancestor::MaXML)"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Unfortunately XSLT 1 does not have a third argument to the key function so there to implement the generic approach with a key you need to include the generate-id(ancestor::MaXML) in the key value:

  <xsl:key name="ref" match="MaXML/Rule/*" use="concat(generate-id(ancestor::MaXML), '|', name())"/>

  <xsl:template match="Body//*[key('ref', concat(generate-id(ancestor::MaXML), '|', name()))]">
      <xsl:copy>
          <xsl:value-of select="key('ref', concat(generate-id(ancestor::MaXML), '|', name()))"/>
      </xsl:copy>
  </xsl:template>

Example at https://xsltfiddle.liberty-development.net/pPqsHTK/4.

Upvotes: 2

Related Questions