XSLT accepts 2 XML files as input and generates XML output file

I am trying to create an XML output file from the main xml file (Input1) based on the data available in the xml solution 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> 

Solution File:

 <TopLevel> <DATA1> <Available>Y</Available> </DATA1> <DATA2> <Available>N</Available> </DATA2> </TopLevel> 

XSLT during processing should output the resulting file (based on YES or NO in the decision file).

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

I must admit that I have never done XML material before, but this is necessary to study the feasibility study. What should be in XSLT? I can use your answers and expand the concept.

Or, if there is an alternative (python, C #, C, C ++, etc.), this is also welcome. I can manage with C / C ++ or any procedure-oriented language.

+4
source share
2 answers

Use document . Pass the URI for the XML solution, for example:

 <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> 
+6
source

Alternatively, here is an XSLT 2.0 solution that can be used with XSLT 2.0 processors such as Saxon 9, AltovaXML, XQSharp:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:param name="dec-file" select="'decision.xml'"/> <xsl:variable name="dec-doc" select="document($dec-file)"/> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="k1" match="TopLevel/*" use="name()"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="Level2[key('k1', LinkedTo, $dec-doc)/Available != 'Y']"/> <xsl:template match="Level2[key('k1', LinkedTo, $dec-doc)/Available = 'Y']/LinkedTo"/> </xsl:stylesheet> 
+2
source

Source: https://habr.com/ru/post/1392299/


All Articles