0
votes

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>
  1. book-name in source.xml matches with group id from properties.xml
  2. store-property name in source.xml matches with variable id from properties.xml
  3. @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,'&#xd;',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,'&#xd;',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>
1
Suggestions please... - kumar
When i add additional child node <store-property name="book2">CLIENT</store-property> the code @Daniel provided is not outputing well. - kumar

1 Answers

1
votes

You could do something like this instead...

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="props" select="document('properties.xml')"/>

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

    <xsl:template match="store-property[matches(.,'^@')]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:value-of select="$props/*/group[@id=current()/../book-name]/variable[@id=current()/@name]/*[local-name()=tokenize(current(),'@')[2]]"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

XML Output (using the supplied XML examples)

<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>
      <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">ab2</store-property>
      <store-property name="book2" type="java.lang.String">rt67</store-property>
   </us-country-factory>
</books>