Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's an interesting problem. I assume that <code>$/some/path/$</code> will always be replaced with the value of the elements returned by an XPath query? I think the "File" must be processed as a string. Yes, it's an XML, but if that pattern holds true, it's much simpler this way. It's just a macro-substitution then.</p> <p>In that case, one solution would be (Scala script):</p> <pre><code>import scala.xml.{Node, NodeSeq} val pattern = """\$([\w/]*)\$""".r def patterns(s: String) = (pattern findAllIn s matchData) map (_ group 1) toList def pathComponents(path: String) = (path split """\b(?!\w)""" toList) map (_ split "\\b" toList) def lookUp(xml: Node, path: List[List[String]]) = { path.foldLeft(xml : NodeSeq) { (nodes, pathComponent) =&gt; pathComponent match { case List("/", component) =&gt; nodes \ component case List("//", component) =&gt; nodes \\ component case _ =&gt; throw new IllegalArgumentException } } map (_ text) } def pathAndValues(s: String, xml: Node) = { patterns(s) map (path =&gt; (path -&gt; lookUp(xml, pathComponents(path)))) } def merge(s: String, xml: Node) = { pathAndValues(s, xml).foldLeft(List(s)) { (files, tuple) =&gt; val (path, values) = tuple for (file &lt;- files; value &lt;- values) yield file replace ("$"+path+"$", value) } } </code></pre> <p>You then read XmlDocument into xml, and the file to be merged into String. This, of course, asssumes the file is not too big to be handled this way. In Scala, it could be done like this:</p> <pre><code>merge(scala.io.Source.fromFile(filename).getLines.mkString, scala.xml.XML.loadFile(XmlDocumentFilename)) </code></pre> <p>That will return a list with every permutation possible for each substitution.</p> <p>If these files are too big to keep in memory, it will be necessary to generate each possible permutation for the values to be substituted, so that you need one pass only to replace all paths for each permutation.</p> <p>If the XPaths are true XPaths, and not just limited to "/" and "//", this solution won't do as is. It will have to be converted to use a true XPath library. Also, note that "/" looks for a child, so if <code>&lt;data&gt;</code> is the root, <code>/data</code> won't work.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload