1
votes

I have an xml, expecting an processed xml via XSLT. Here, I could generate the xml by applying template and now I want to use this template output as an input(dynamically) for the another input within the same XSL.

i.e. I want to pass the current output as input to the template output2.

Output1 : first Template, resulting R1
Output2 : second Template, needs to generate the final output.

INPUT XML

   <rules>
    <emie>
        <domain exclude="true">sww-epw3.testing.com</domain>
        <domain>sww-epw3.testing.com

            <path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang">/Apps/dsprm</path>
            <path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang">/Apps/uaprm</path>
            <path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang">/Apps/ptprm</path>
            <path docMode="7" comment="APEXID:141923;MyRequestID:7512579;AppName:Production Revenue Accounting-PRA;StepOut:4 March 2016;StepIn:28 February 2017;Requester:[email protected];BusinessUnit:UI;PortfolioManager:[email protected];Remarks:On Board Application">/apps/doto</path>
            <path docMode="7" comment="APEXID:;MyRequestID:7219068;AppName:;StepOut:9 December 2015;StepIn:;Requester:[email protected];BusinessUnit:UA;PortfolioManager:;Remarks:Technical Assessment">/apps/seprm</path>
            <path docMode="7" comment="APEXID:;MyRequestID:7219068;AppName:;StepOut:9 Dec 2015;StepIn:;Requester:[email protected];BusinessUnit:UA;PortfolioManager:;Remarks:Technical Assessment">/apps/gfprm</path>
        </domain>
    </emie>
</rules>

**XSLT **

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:copy-of select="$R2"/>
    </xsl:template>
    <xsl:variable name="R1">
        <xsl:call-template name="output1" />
    </xsl:variable>
    <xsl:variable name="R2">
        <xsl:call-template name="output2" />
    </xsl:variable>
    <xsl:template name="output2">
        <xsl:element name="DOMAIN">
            <xsl:element name="APEXID"></xsl:element>
            <xsl:element name="MYREQUESTID"></xsl:element>
            <xsl:element name="APPLICATION_NAME"></xsl:element>
            <xsl:element name="URL"></xsl:element>
            <xsl:element name="MODE"></xsl:element>
            <xsl:element name="STEPOUT"></xsl:element>
            <xsl:element name="STEPIN"></xsl:element>
            <xsl:element name="BU"></xsl:element>
            <xsl:element name="PORTFOLIO_MGR"></xsl:element>
            <xsl:element name="REMARKS"></xsl:element>
            <xsl:element name="RAG_STATUS"></xsl:element>
            <xsl:element name="APEX_DATA_PORTAL_URL"></xsl:element>
        </xsl:element>
    </xsl:template>
    <xsl:template name="output1">
        <xsl:variable name="site">
            <xsl:value-of select="rules/emie/domain/text()"/>
        </xsl:variable>
        <emie>
            <xsl:for-each select="rules/emie/domain/path">
                <domain>
                    <xsl:element name="URL">
                        <xsl:value-of select="concat($site,text())" />
                    </xsl:element>
                    <xsl:apply-templates select="@comment"/>
                </domain>
            </xsl:for-each>
            <xsl:for-each select="domain">
                <domain>
                    <xsl:element name="URL">
                        <xsl:value-of select="concat($site,text())" />
                    </xsl:element>
                    <xsl:apply-templates select="@comment"/>
                </domain>
            </xsl:for-each>
        </emie>
    </xsl:template>
    <xsl:template match="@comment" name="tokenize">
        <xsl:param name="text" select="."/>
        <xsl:param name="separator" select="';'"/>
        <xsl:param name="separator2" select="':'"/>
        <xsl:choose>
            <xsl:when test="not(contains($text, $separator))">
                <xsl:choose>
                    <xsl:when test="(contains($text, $separator2))">
                        <xsl:variable name="token" select="normalize-space(substring-before($text, $separator2))" />
                        <xsl:element name="{$token}">
                            <xsl:value-of select="normalize-space(substring-after($text, $separator2))"/>
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:element name="AppName">
                            <xsl:value-of select="$text"/>
                        </xsl:element>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-before($text, $separator)"/>
                </xsl:call-template>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

CURRENT OUTPUT

<emie>
    <domain>
        <URL>sww-epw3.testing.com/Apps/dsprm</URL>
        <APEXID>102994</APEXID>
        <AppName>Physical Records Management</AppName>
        <StepOut>22 May 2015</StepOut>
        <StepIn>22 May 2016</StepIn>
        <Requester>Per Roseth</Requester>
        <BusinessUnit>Upstream</BusinessUnit>
        <PortfolioManager>Junfeng Liang</PortfolioManager>
    </domain>
    <domain>
        <URL>sww-epw3.testing.com/Apps/uaprm</URL>
        <APEXID>102994</APEXID>
        <AppName>Physical Records Management</AppName>
        <StepOut>22 May 2015</StepOut>
        <StepIn>22 May 2016</StepIn>
        <Requester>Per Roseth</Requester>
        <BusinessUnit>Upstream</BusinessUnit>
        <PortfolioManager>Junfeng Liang</PortfolioManager>
    </domain>
    <domain>
        <URL>sww-epw3.testing.com/Apps/ptprm</URL>
        <APEXID>102994</APEXID>
        <AppName>Physical Records Management</AppName>
        <StepOut>22 May 2015</StepOut>
        <StepIn>22 May 2016</StepIn>
        <Requester>Per Roseth</Requester>
        <BusinessUnit>Upstream</BusinessUnit>
        <PortfolioManager>Junfeng Liang</PortfolioManager>
    </domain>
    <domain>
        <URL>sww-epw3.testing.com/apps/doto</URL>
        <APEXID>141923</APEXID>
        <MyRequestID>7512579</MyRequestID>
        <AppName>Production Revenue Accounting-PRA</AppName>
        <StepOut>4 March 2016</StepOut>
        <StepIn>28 February 2017</StepIn>
        <Requester>[email protected]</Requester>
        <BusinessUnit>UI</BusinessUnit>
        <PortfolioManager>[email protected]</PortfolioManager>
        <Remarks>On Board Application</Remarks>
    </domain>
    <domain>
        <URL>sww-epw3.testing.com/apps/seprm</URL>
        <APEXID></APEXID>
        <MyRequestID>7219068</MyRequestID>
        <AppName></AppName>
        <StepOut>9 December 2015</StepOut>
        <StepIn></StepIn>
        <Requester>[email protected]</Requester>
        <BusinessUnit>UA</BusinessUnit>
        <PortfolioManager></PortfolioManager>
        <Remarks>Technical Assessment</Remarks>
    </domain>
    <domain>
        <URL>sww-epw3.testing.com/apps/gfprm</URL>
        <APEXID></APEXID>
        <MyRequestID>7219068</MyRequestID>
        <AppName></AppName>
        <StepOut>9 Dec 2015</StepOut>
        <StepIn></StepIn>
        <Requester>[email protected]</Requester>
        <BusinessUnit>UA</BusinessUnit>
        <PortfolioManager></PortfolioManager>
        <Remarks>Technical Assessment</Remarks>
    </domain>
</emie>

EXPECTED OUTPUT- Need to include more new elements and process the data of input xml (output1) e.g date formating

    <emie><domain>
        <URL>sww-epw3.testing.com/apps/gfprm</URL>
        <APEXID></APEXID>
        <MyRequestID>7219068</MyRequestID>
        <AppName></AppName>
        <StepOut>9 Dec 2015</StepOut>
        <StepIn></StepIn>
        <Requester>[email protected]</Requester>
        <BusinessUnit>UA</BusinessUnit>
        <PortfolioManager></PortfolioManager>
        <Remarks>Technical Assessment</Remarks> <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
    </domain>
  <domain>
        <URL>sww-epw3.testing.com/apps/gfprm</URL>
        <APEXID></APEXID>
        <MyRequestID>7219068</MyRequestID>
        <AppName></AppName>
        <StepOut>9 Dec 2015</StepOut>
        <StepIn></StepIn>
        <Requester>[email protected]</Requester>
        <BusinessUnit>UA</BusinessUnit>
        <PortfolioManager></PortfolioManager>
        <Remarks>Technical Assessment</Remarks> <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
    </domain>
  <domain>
        <URL>sww-epw3.testing.com/apps/gfprm</URL>
        <APEXID></APEXID>
        <MyRequestID>7219068</MyRequestID>
        <AppName></AppName>
        <StepOut>9 Dec 2015</StepOut>
        <StepIn></StepIn>
        <Requester>[email protected]</Requester>
        <BusinessUnit>UA</BusinessUnit>
        <PortfolioManager></PortfolioManager>
        <Remarks>Technical Assessment</Remarks> <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
    </domain></emie>
1
What is your expected output? And why do you think it requires two-pass processing?michael.hor257k
I want to add few more tags to the output1 and need to format the date as dd/mm/yyy. ` <domain> <URL>sww-epw3.testing.com/apps/gfprm</URL> <APEXID></APEXID> <MyRequestID>7219068</MyRequestID> <AppName></AppName> <StepOut>9 Dec 2015</StepOut> <StepIn></StepIn> <Requester>[email protected]</Requester> <BusinessUnit>UA</BusinessUnit> <PortfolioManager></PortfolioManager> <Remarks>Technical Assessment</Remarks> <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL> </domain> </emie>`user1093452
Please don't post code in comments - edit your question instead. -- See also: minimal reproducible example.michael.hor257k
The XSLT that you have posted does not produce the claimed output; it results in an error (Element name is not a valid QName). I am having trouble understanding what your question is.michael.hor257k
I am using an online tool (utilities-online.info/xsltransformation) to test my XSLT aganist the input XML and its working for the above inputs and results the output1user1093452

1 Answers

0
votes

I would suggest you use this as your starting point:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

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

<xsl:template match="/rules">
    <xsl:variable name="site" select="emie/domain[1]/text()" />
    <!-- first pass -->
    <xsl:variable name="first-pass-rtf">
        <xsl:for-each select="emie/domain/path">
            <domain>
                <URL>
                    <xsl:value-of select="concat($site, .)" />
                </URL>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="@comment"/>
                </xsl:call-template>
                <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
            </domain>
        </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="first-pass" select="exsl:node-set($first-pass-rtf)" />      
    <!-- output -->
    <emie>
        <xsl:apply-templates select="$first-pass"/>
    </emie>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="';'"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:if test="$token">
            <xsl:element name="{substring-before($token, ':')}">
                <xsl:value-of select="substring-after($token, ':')"/>
            </xsl:element>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

As it is, it will produce the following output when applied to your input example:

<emie>
   <domain>
      <URL>sww-epw3.testing.com/Apps/dsprm</URL>
      <APEXID>102994</APEXID>
      <AppName>Physical Records Management</AppName>
      <StepOut>22 May 2015</StepOut>
      <StepIn>22 May 2016</StepIn>
      <Requester>Per Roseth</Requester>
      <BusinessUnit>Upstream</BusinessUnit>
      <PortfolioManager>Junfeng Liang</PortfolioManager>
      <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
   </domain>
   <domain>
      <URL>sww-epw3.testing.com/Apps/uaprm</URL>
      <APEXID>102994</APEXID>
      <AppName>Physical Records Management</AppName>
      <StepOut>22 May 2015</StepOut>
      <StepIn>22 May 2016</StepIn>
      <Requester>Per Roseth</Requester>
      <BusinessUnit>Upstream</BusinessUnit>
      <PortfolioManager>Junfeng Liang</PortfolioManager>
      <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
   </domain>
   <domain>
      <URL>sww-epw3.testing.com/Apps/ptprm</URL>
      <APEXID>102994</APEXID>
      <AppName>Physical Records Management</AppName>
      <StepOut>22 May 2015</StepOut>
      <StepIn>22 May 2016</StepIn>
      <Requester>Per Roseth</Requester>
      <BusinessUnit>Upstream</BusinessUnit>
      <PortfolioManager>Junfeng Liang</PortfolioManager>
      <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
   </domain>
   <domain>
      <URL>sww-epw3.testing.com/apps/doto</URL>
      <APEXID>141923</APEXID>
      <MyRequestID>7512579</MyRequestID>
      <AppName>Production Revenue Accounting-PRA</AppName>
      <StepOut>4 March 2016</StepOut>
      <StepIn>28 February 2017</StepIn>
      <Requester>[email protected]</Requester>
      <BusinessUnit>UI</BusinessUnit>
      <PortfolioManager>[email protected]</PortfolioManager>
      <Remarks>On Board Application</Remarks>
      <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
   </domain>
   <domain>
      <URL>sww-epw3.testing.com/apps/seprm</URL>
      <APEXID/>
      <MyRequestID>7219068</MyRequestID>
      <AppName/>
      <StepOut>9 December 2015</StepOut>
      <StepIn/>
      <Requester>[email protected]</Requester>
      <BusinessUnit>UA</BusinessUnit>
      <PortfolioManager/>
      <Remarks>Technical Assessment</Remarks>
      <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
   </domain>
   <domain>
      <URL>sww-epw3.testing.com/apps/gfprm</URL>
      <APEXID/>
      <MyRequestID>7219068</MyRequestID>
      <AppName/>
      <StepOut>9 Dec 2015</StepOut>
      <StepIn/>
      <Requester>[email protected]</Requester>
      <BusinessUnit>UA</BusinessUnit>
      <PortfolioManager/>
      <Remarks>Technical Assessment</Remarks>
      <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
   </domain>
</emie>

To further modify the output, you only need to add templates matching the nodes you want to modify. For example, to insert a node after APEXID, add the following template:

<xsl:template match="APEXID">
    <xsl:copy-of select="."/>
    <MyRequestID>7219068</MyRequestID>
</xsl:template>

Added:

I cannot predict the token names of inout file.so i need to generate an XML with uniform elements in the finaly XML.

To list the required elements explicitly, add the following template:

<xsl:template match="domain">
    <xsl:copy>
        <URL>
            <xsl:value-of select="URL" />
        </URL>
        <APEXID>
            <xsl:value-of select="APEXID" />        
        </APEXID>
        <MyRequestID>
            <xsl:value-of select="MyRequestID" />        
        </MyRequestID>
        <AppName>
            <xsl:value-of select="AppName" />        
        </AppName>
        <StepOut>
            <xsl:value-of select="StepOut" />
        </StepOut>
        <StepIn>
            <xsl:value-of select="StepIn" />
        </StepIn>
        <Requester>
            <xsl:value-of select="Requester" />
        </Requester>
        <BusinessUnit>
            <xsl:value-of select="BusinessUnit" />
        </BusinessUnit>
        <PortfolioManager>
            <xsl:value-of select="PortfolioManager" />
        </PortfolioManager>
        <Remarks>
            <xsl:value-of select="Remarks" />
        </Remarks>
        <APEX_DATA_PORTAL_URL>
            <xsl:value-of select="APEX_DATA_PORTAL_URL" />
        </APEX_DATA_PORTAL_URL>
    </xsl:copy>
</xsl:template>