4

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.

0

2 Answers 2

6

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>
Sign up to request clarification or add additional context in comments.

1 Comment

My apologies, I have been off stackoverflow for a while now. Both solutions(Kirill and Martin) worked equally well for me with a little tweak. I am also investigating other methodologies such as UML modeling for my problem statement.
2

As an alternative, here is an XSLT 2.0 solution that can be used with XSLT 2.0 processors like 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.