1

I want to know if it's possible to take the values for the same resources (same attribute value) from another file, maintaining in the first file all the structure and comments.

Maybe I'll explain it better with an example.

Input file 1 (the one that needs the values):

<?xml version="1.0" encoding="UTF-8"?>
<root>
     <element name="1">File1-value1</frag>
     <element name="2">File1-value2</frag>
     <element name="3">File1-value3</frag>
</root>

Input file 2 (the one to take the values from):

<?xml version="1.0" encoding="UTF-8"?>
<root>

     <element name="3">File2-value3</frag>
     <element name="7">File2-value3</frag>

     <element name="1">File2-value1</frag>
     <element name="2">File2-value2</frag>

</root>

Desired output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
     <element name="1">File2-value1</frag>
     <element name="2">File2-value2</frag>
     <element name="3">File2-value3</frag>
</root>

The point is to have all the contents of file 2 in file 1 for matching attributes (there will be extra elements with attribute values not present in file 1 that I don't want) but preserving order, tab structure, spaces and comments of file 1.

It may seem a very dumb process but there are a lot of big files. I have been reading a lot about XSLT but I could not found any solution as I'm completely novice with it.

Thank you very much for any possible answers.

4
  • 2
    Which Xslt version do you want to use? Commented Mar 14, 2014 at 21:16
  • Can we assume that <element name="7">File2-value3</frag> should instead say <element name="7">File2-value7</frag>? Commented Mar 14, 2014 at 21:37
  • Both versions are Ok for me, thank you very much @martin-honnen your solution works perfect and it's exactly what I wanted. Commented Mar 16, 2014 at 11:51
  • Thank you for your interest @LarsH. My point was to match the names in between both files and ignore the values that are only in file 2. My example was not very clear, but item contents are not related with attribute names. Commented Mar 16, 2014 at 11:53

2 Answers 2

2

Xslt 2.0

<xsl:key name="k1" match="element" use="@name"/>

<xsl:param name="lkp-url" select="'lookup.xml'"/>

<xsl:variable name="lkp-doc" select="doc($lkp-url)"/>

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

<xsl:template match="element[@name and key('k1', @name, $lkp-doc)]">
  <xsl:copy>
    <xsl:copy-of select="@* , key('k1', @name, $lkp-doc)/node()"/>
  </xsl:copy>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

0

For any newbies, like me, who are struggling to run this, a few amendments are required. All elements named element needs </frag> to be replaced with </element>

And, for me, the chosen answer needs to be encapsulated with:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 ....
 </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.