Is there a way to embed one code snippet into another?
Asked Answered
H

1

6

Let's assume that I have

  • snippet A
  • snippet B
    where snippet A contains snippet B n times with n > 1.

Right now I have copied the content of snippet B into snippet A. This has the disadvantage, that whenever I change snippet B, I have to additionally change snippet A. Therefore, my question is whether there is some kind of statement to embed one snippet into another?
e.g.
<externalsnippet src=".\snippetB.snippet" />
or something similar.

Headless answered 12/8, 2010 at 11:48 Comment(0)
C
1

You could use an external parsed general entity to declare an entity reference for snippet B and then use it n number of times inside of snippet A.

When snippet A is parsed, the entity references will be expanded and the content from snippet B will be included at each spot where the entity was used.

For example, assume that you had a file called snipppetB.xml:

<snippetB>
  <foo>Content goes here</foo>
</snippetB>

And a file for snippet A declared an entity called snippetB referencing snippetB.xml and used it four times:

<!DOCTYPE snippetA [
   <!ENTITY snippetB SYSTEM "./snippetB.xml">
]>
<snippetA>
<a>&snippetB;</a>
<b>&snippetB;</b>
<c>&snippetB;</c>
<d>&snippetB;</d>
</snippetA>

When snippetA.xml is parsed, the XML content would look like this:

 <snippetA>
 <a>
 <snippetB>
  <foo>Content goes here</foo> 
  </snippetB>
  </a>
 <b>
 <snippetB>
  <foo>Content goes here</foo> 
  </snippetB>
  </b>
 <c>
 <snippetB>
  <foo>Content goes here</foo> 
  </snippetB>
  </c>
 <d>
 <snippetB>
  <foo>Content goes here</foo> 
  </snippetB>
  </d>
  </snippetA>
Chili answered 4/2, 2012 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.