licensedlice
licensedlice

Reputation: 65

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

Let's assume that I have

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.

Upvotes: 6

Views: 114

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66783

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>

Upvotes: 1

Related Questions