Applying patterns multiple times based on string values ​​from another document

I have two XML

File1.xml It contains the mapping for the IP address, which must be copied several times and replaced with the value specified.

<baseNode> <internal ip="192.168.1.1"> <someOtherTags>withsome values which should not be changed</someOtherTags> </internal> <internal ip="192.168.1.2"> <someOtherTags>withsome more values which should not be changed</someOtherTags> </internal> </baseNode> 

File2.xml

  <IPMappings> <sourceIP value="192.168.1.1"> <replacement>10.66.33.22</replacement> <replacement>10.66.33.44</replacement> </sourceIP> <sourceIP value="192.168.1.2"> <replacement>10.66.34.22</replacement> <replacement>10.66.34.44</replacement> </sourceIP> </IPMappings> 

The resulting XML should be:

  <baseNode> <internal ip="10.66.33.22"> <someOtherTags>withsome values which should not be changed</someOtherTags> </internal> <internal ip="10.66.33.44"> <someOtherTags>withsome values which should not be changed</someOtherTags> </internal> <internal ip="10.66.34.22"> <someOtherTags>withsome more values which should not be changed</someOtherTags> </internal> <internal ip="10.66.34.44"> <someOtherTags>withsome more values which should not be changed</someOtherTags> </internal> </baseNode> 

How can I use XSLT for this?

0
source share
1 answer

Here is my suggestion, the first document is the primary input document, the second document is passed as a parameter:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="map-doc-url" select="'File2.xml'"/> <xsl:variable name="map-doc" select="document($map-doc-url)"/> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="ip" match="sourceIP" use="@value"/> <xsl:template match="baseNode"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="baseNode/internal"> <xsl:variable name="this" select="."/> <xsl:for-each select="$map-doc"> <xsl:apply-templates select="key('ip', $this/@ip)/replacement"> <xsl:with-param name="internal" select="$this"/> </xsl:apply-templates> </xsl:for-each> </xsl:template> <xsl:template match="sourceIP/replacement"> <xsl:param name="internal"/> <internal ip="{.}"> <xsl:copy-of select="$internal/node()"/> </internal> </xsl:template> </xsl:stylesheet> 

When I run above with Saxon 6.5.5, I get the following result:

 <baseNode> <internal ip="10.66.33.22"> <someOtherTags>withsome values which should not be changed</someOtherTags> </internal> <internal ip="10.66.33.44"> <someOtherTags>withsome values which should not be changed</someOtherTags> </internal> <internal ip="10.66.34.22"> <someOtherTags>withsome more values which should not be changed</someOtherTags> </internal> <internal ip="10.66.34.44"> <someOtherTags>withsome more values which should not be changed</someOtherTags> </internal> </baseNode> 
+1
source

All Articles