XSLT 在一个 XML 中查找值并在另一个 XML 文件中替换 [英] XSLT to lookup values in one XML and replace in another XML file

查看:25
本文介绍了XSLT 在一个 XML 中查找值并在另一个 XML 文件中替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的其中一个产品能够将配置信息输出为 XML,但是,在该输出文件中,它们没有包含实际的主机标识符(名称),而是对每个标识符使用一种 GUID 引用.

One of the products we work with is able to output configuration information as XML, but, in that output file, instead of including actual host identifiers (names), they use a kind of GUID reference for each of the identifiers.

我制作了一个 XML 文件,其中包含主机标识符 GUID 和实际主机标识符(查找)之间的映射",我想实现一个 XSLT,它将通过配置文件并替换所有主机标识符带有主机标识符名称的 GUID,它将从我制作的其他 XML 文件 (lookup.xml) 中查找.

I have made an XML file that contains the "mapping" between the host identifiers GUIDs and the actual host identifiers(lookup), and I want to implement an XSLT that will go through the configuration file and replace all of the host identifier GUIDs with the host identifier names, which it would lookup from the other XML file I made (lookup.xml).

lookup.xml 文件如下所示:

Here's what the lookup.xml file looks like:

<?xml version="1.0"?>
<hostids>

  <hostid name="e687903c-d185-4560-9117-c60f386b76c1">Agent</hostid>
  <hostid name="b9230962-13ca-4d23-abf8-d3cd1ca4dffc">test2</hostid>

</hostids>

这是配置文件的样子(我通过一些处理运行了原始文件以获得这个):

and here is what the configuration file looks like (I ran the original file through some processing to get this):

<?xml version="1.0"?>
<resources>

  <resource><host>e687903c-d185-4560-9117-c60f386b76c1</host><url>/console/**</url></resource>
  <resource><host>b9230962-13ca-4d23-abf8-d3cd1ca4dffc</host><url>/ServiceByName</url></resource>

</resources>

这里是输出的样子:

<?xml version="1.0"?>
<resources>

  <resource><host>Agent</host><url>/console/**</url></resource>
  <resource><host>test2</host><url>/ServiceByName</url></resource>

</resources>

我正在 RedHat 机器上使用 xsltproc,我认为是 XSLT 1.0.

I am working with xsltproc on a RedHat machine, which is XSLT 1.0, I think.

我已经尝试使用我在这里找到的几个不同的示例 XSLT 来实现这一点,例如:

I have tried to get this working with several different example XSLTs that I found here, e.g.:

XSLT替换"值与另一个按属性匹配的文件

但一直无法让他们中的任何一个工作.

but haven't been able to get any of them working.

谁能提供一个 XSLT 1.0 示例来实现这一点?

Can anyone provide an XSLT 1.0 example that might be able to accomplish this?

附言这是另一个包含示例的线程,而 XSLT 1.0 示例对我不起作用.当我运行它时(在修改以匹配我的元素名称等之后),看起来它只是将整个原始 XML 包装在 .

P.S. This is another thread that had examples and the XSLT 1.0 examples wouldn't work for me. When I ran it (after modifying to match my element names, etc.), it looks like it just wrapped the entire original XML in .

如何使用另一个 XML 文件中的属性值作为当前 XML 中的元素值选择

推荐答案

试试这个方法?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="path-to-lookup" select="'lookup.xml'" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="host">
    <xsl:copy>
        <xsl:value-of select="document($path-to-lookup)/hostids/hostid[@name = current()]" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

<小时>

或者,如果您愿意:


Or, if you prefer:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="path-to-lookup" select="'lookup.xml'" />

<xsl:key name="host" match="hostid" use="@name" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="host">
    <xsl:copy>
        <xsl:variable name="host-id" select="." />
        <!-- switch context to lookup document in order to use key -->
        <xsl:for-each select="document($path-to-lookup)">
            <xsl:value-of select="key('host', $host-id)" />
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这篇关于XSLT 在一个 XML 中查找值并在另一个 XML 文件中替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆