有没有办法将一个代码片段嵌入到另一个代码片段中?

发布于 2024-09-14 05:14:55 字数 421 浏览 3 评论 0原文

假设我有

  • 代码段 A
  • 代码段 B
    其中 片段 A 包含 片段 B n 次 其中 n > 1

现在我已将片段 B 的内容复制到片段 A 中。这样做的缺点是,每当我更改代码段 B 时,我都必须另外更改代码段 A。因此,我的问题是是否有某种语句可以将一个片段嵌入到另一个片段中?
例如

或类似的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2024-09-21 05:14:55

您可以使用外部解析的通用实体来声明代码段 B 的实体引用,然后在代码段 A 中使用它 n 次。

当代码段 A 被解析时,实体引用将被扩展,并且片段 B 中的内容将包含在使用该实体的每个位置。

例如,假设您有一个名为 snipppetB.xml 的文件:

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

snippet A 的文件声明了一个名为 snippetB 引用的实体snippetB.xml 并使用了四次:

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

解析 snippetA.xml 时,XML 内容将如下所示:

 <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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文