我可以在 XML 文件中使用变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34777149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:10:32  来源:igfitidea点击:

Can I use variables in XML files?

xml

提问by cbr

Is it possible to define variables in an XML file?

是否可以在 XML 文件中定义变量?

For example:

例如:

VARIABLE = 'CHIEF_NAME'

VARIABLE = 'CHIEF_NAME'

<foods>
  <food>
    <name>French Toast</name>
    <price>.50</price>
    <calories>600</calories>
    <chief>VARIABLE</chief>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>.95</price>
    <calories>950</calories>
    <chief>VARIABLE</chief>
  </food>
</foods>

采纳答案by kjhughes

You can declare an entity referencefor chiefand reference it as &chief;:

你可以声明一个实体引用chief和其引用为&chief;

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE foods [
  <!ENTITY chief "CHIEF_NAME!">
  <!-- .... -->
]>
<foods>
  <food>
    <name>French Toast</name>
    <price>.50</price>
    <calories>600</calories>
    <chief>&chief;</chief>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>.95</price>
    <calories>950</calories>
    <chief>&chief;</chief>
  </food>
</foods>

回答by Jared

Thats what XSLT is for.

这就是 XSLT 的用途。

XSLT Wiki

XSLT 维基

Something to get you started:

一些让你开始的东西:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="yourVar" select="'CHIEF_NAME'">
</xsl:variable>
<xsl:template match="/">
  <food>
    <name>French Toast</name>
    <price>.50</price>
    <calories>600</calories>
    <chief><xsl:copy-of select="$yourVar" /></chief>
  </food>
</xsl:template>

</xsl:stylesheet>

This syntax is not exactly correct, but I think generally this is the direction you should seek

这种语法并不完全正确,但我认为通常这是您应该寻求的方向