Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并2个XML文件,合并键并将其作为模板应用_Xml_Xslt - Fatal编程技术网

合并2个XML文件,合并键并将其作为模板应用

合并2个XML文件,合并键并将其作为模板应用,xml,xslt,Xml,Xslt,我必须将XML文件归档:language.XML和menu.XML。默认情况下,第一个加载,第二个加载 language.xml: <?xml version="1.0" encoding="utf-8"?> <language> <header> <menu> <title>Title of example</title> </menu>

我必须将XML文件归档:language.XMLmenu.XML。默认情况下,第一个加载,第二个加载

language.xml:

<?xml version="1.0" encoding="utf-8"?>
<language>
    <header>
        <menu>
            <title>Title of example</title>
        </menu>
        <menu>
            <title>Title of example 2</title>
        </menu>
        <menu>
            <title>Title of example 3</title>
        </menu>
    </header>
</language>

示例标题
例2的标题
例3的标题
menu.xml

<?xml version="1.0" encoding="utf-8"?>
<header>
    <menu>
        <a>/example</a>
    </menu>
    <menu>
        <a>/example2</a>
    </menu>
    <menu>
        <a>/example3</a>
    </menu>
</header>


谢谢

据我所知,您所说的文件应该根据其作为XML子节点的位置进行链接。在这种情况下,您需要以下内容:

<xsl:for-each select="menu">
  <a href="{.}">
    <xsl:variable name="position"><xsl:value-of select="position()"/></xsl:variable>
    <xsl:for-each select="$param//menu[position() = $position]">
      <xsl:value-of select="title"/>
    </xsl:for-each>
  </a>
</xsl:for-each>

使用匹配模板,您可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="localization" select="document('index.en.xml')" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="menu">
    <a href="{a}">
      <xsl:variable name="pos" select="position()" />
      <xsl:value-of select="$localization/language/header/menu[$pos]/title"/>
    </a>
  </xsl:template>

</xsl:stylesheet>

试验

看起来好像您发布了两次相同的输入文档。“示例2”应该来自哪里?它不在你发布的输入中。在哪里定义URL和链接文本之间的映射?似乎也缺少某种索引/键。@0xA3对不起!输入错误,我正在手写输出。基本上,language.xml有锚标题,menu.xml有锚的href。谢谢映射是如何定义的?位置是否简单,即第一个菜单/a映射到第一个菜单/标题,第二个映射到第二个,等等?是的!就这样!你认为如果每个菜单上都有id会更好吗,2, 3... 对我来说,这是不需要的,但它更容易,不是一个问题!Thanks@0xA3:这是正确的答案,但我认为它有一个输入错误:
apply imports
应该是
apply templates
。@Alejandro:谢谢你的提示。它可能是由于自动完成而发生的,我没有注意到它,因为它也可以工作(但没有意义)。@0xA3:另外,+1表示使用模式匹配的答案。文档()中可能有一个变量?我试过了,但似乎不起作用。比如:
,然后是
。提前谢谢@Isern Palaus:这是可能的,但它要求您使用
concat()
函数显式连接字符串(这与PHP或Perl等语言不同):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="localization" select="document('index.en.xml')" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="menu">
    <a href="{a}">
      <xsl:variable name="pos" select="position()" />
      <xsl:value-of select="$localization/language/header/menu[$pos]/title"/>
    </a>
  </xsl:template>

</xsl:stylesheet>