XSLT to lookup values in one XML and replace in another XML file

寵の児 提交于 2019-12-06 09:59:35

Try it this way?

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!