I am working with a problem and was wondering how than can be done using XSLT(1.0). I have combined the data from 2 XML files into a single one. I have to inject values from one XML node into the other. Since my XML files are pretty big, I don't have to luxury of of creating elements in the XSLT. I would simply like to somehow map them from one data1 elements to data2 elements.
Input:
<combinedData>
<data1>
<element1>
<id>12</id>
<name>Tony Green</name>
<address>Home Address</address>
</element1>
</data1>
<data2>
<element1>
<element2>
<element3>
<IdOfPerson></IdOfPerson>
<NameOfPerson></NameOfPerson>
<addressOfPerson></addressOfPerson>
</element3>
</element2>
</element1>
</data2>
Desired Output:
<data2>
<element1>
<element2>
<element3>
<IdOfPerson>12</IdOfPerson>
<NameOfPerson>Tony Green</NameOfPerson>
<addressOfPerson>Home Address</addressOfPerson>
</element3>
</element2>
</element1>
Any help with this will be highly appreciated.
Use the identity transform to copy data2
:
<xsl:template match="/">
<xsl:apply-templates select="//data2"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
Use a specific template for the child nodes of element3
. I am assuming that you want to copy the data from the lower-case nodes of the same name of data1/element1
:
<xsl:template match="element3/node()">
<xsl:variable name="name-without-of-person" select="substring-before(name(), 'OfPerson')" />
<xsl:variable name="lower-case" select="translate($name-without-of-person, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
<!-- copy the original node -->
<xsl:copy>
<!-- XSLT 1.0 does not support XPath queries that are dynamically generated,
therefore, .../element1/$lower-case does not work.
However, using a predicate with a name() query works -->
<xsl:value-of select="/combinedData/data1/element1/*[name()=$lower-case]"/>
</xsl:copy>
</xsl:template>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.