I am trying to replace a variable @mybook.01@ in source file with the values from properties.xml that matches the book-name element in source file with the group id in properties.xml
Here is my source file:
<books>
<us-country-factory>
<book-name>books/props/Classic</book-name>
<store-property name="book1" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2">CLIENT</store-property>
</us-country-factory>
<us-country-factory>
<book-name>books/props/Classic1</book-name>
<store-property name="book1" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2" type="java.lang.String">@mybook.01@</store-property>
</us-country-factory>
</books>
and Here is my properties.xml file:
<variables>
<group id="books/props/Classic">
<variable id="book1">
<mybook.01>123</mybook.01>
</variable>
<variable id="book2">
<mybook.01>789</mybook.01>
</variable>
</group>
<group id="books/props/Classic1">
<variable id="book1">
<mybook.01>ab2</mybook.01>
</variable>
<variable id="book2">
<mybook.01>rt67</mybook.01>
</variable>
</group>
</variables>
So the Expected output would looks like this:
<books>
<us-country-factory>
<book-name>books/props/Classic</book-name>
<store-property name="book1" type="java.lang.String">123</store-property>
<store-property name="book2" type="java.lang.String">789</store-property>
</us-country-factory>
<us-country-factory>
<book-name>books/props/Classic1</book-name>
<store-property name="book1" type="java.lang.String">ab2</store-property>
<store-property name="book2" type="java.lang.String">rt67</store-property>
</us-country-factory>
</books>
book-name in source.xml matches withgroup idfrom properties.xmlstore-propertyname in source.xml matches withvariable idfrom properties.xml@mybook.01@from source.xml is replaced with value of<mybook.01>from properties.xml
I was able to get this when there is only group in properties.xml and one chile node, but not sure how to loop the groups with the matched templates.
Here is my xsl file:
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>
<xsl:key name="props" match="variable/*"
use="concat(../@id,'
',name(.))"/>
<xsl:template match="book-name">
<xsl:apply-templates select="store-property"/>
</xsl:template>
<xsl:template match="store-property">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="id" select="@name"/>
<xsl:analyze-string select="." regex="@(.*?)@">
<xsl:matching-substring>
<xsl:value-of select="key('props',concat($id,'
',regex-group(1)),
doc('properties.xml'))"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<!--identity for all other nodes-->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<store-property name="book2">CLIENT</store-property>the code @Daniel provided is not outputing well. - kumar