Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT taking 2 XML files as input and generating output XML file

Tags:

xml

xslt

xpath

I am trying to generate an output XML file from a master xml file (Input1) based on data available in a decision xml file (Input2).

Master file

<Level1>

 <Level2>
  <LinkedTo>DATA1</LinkedTo> <!DATA1 in the decision file>
  <Attribute1>1</Attribute1>
  <Attribute2>2</Attribute2>
 </Level2>

 <Level2>
  <LinkedTo>DATA2</LinkedTo>
  <Attribute1>3</Attribute1>
  <Attribute2>4</Attribute2>
 </Level2>

</Level1>

Decision File:

<TopLevel>
 <DATA1>
  <Available>Y</Available>
 </DATA1>

 <DATA2>
  <Available>N</Available>
 </DATA2>

</TopLevel>

The XSLT when processed must output resultant file (Based on a YES or a NO in the decision file).

<Level1>
 <Level2>
  <Attribute1>1</Attribute1>
  <Attribute2>2</Attribute2>
 </Level2>
</Level1>

I must confess I have never done XML stuff before, but this is needed for a feasibility study. What should be in the XSLT? I can use your answers and extend the concept.

Or if there is an alternative (python,C#,C,C++ etc), those are welcome as well. I can manage with C/C++ or any procedure oriented language.

like image 475
Raj Avatar asked Feb 21 '23 18:02

Raj


1 Answers

Use document function. Pass URI to decision XML, e.g.:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="Level1">
    <xsl:copy>
      <xsl:apply-templates select="Level2"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Level2">
    <xsl:if test="document('Decision.xml')/TopLevel/*[
        name() = current()/LinkedTo and Available = 'Y']">
      <xsl:copy>
        <xsl:apply-templates select="*[not(self::LinkedTo)]"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>
like image 79
Kirill Polishchuk Avatar answered Feb 24 '23 07:02

Kirill Polishchuk