【问题标题】:XSLT to lookup values in one XML and replace in another XML fileXSLT 在一个 XML 中查找值并在另一个 XML 文件中替换
【发布时间】:2016-07-28 16:46:04
【问题描述】:

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

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

lookup.xml 文件如下所示:

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

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

</hostids>

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

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

我已尝试使用我在此处找到的几个不同示例 XSLT 来实现此功能,例如:

XSLT "replace" values with another file matching by attribute

但无法让它们中的任何一个工作。

任何人都可以提供一个 XSLT 1.0 示例来实现这一点吗?

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

How to use attribute values from another XML file as an element value selection in the current XML

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    试试这个方法?

    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>
    

    或者,如果您愿意:

    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>
    

    【讨论】:

    • michael.hor257k - 我尝试了您的第一个 XSLT,它运行良好!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多