有些XML文件包含来自不同系统的映射数据。
使用xslt 1.0将xml输入文件中的数据替换为其他系统中的数据的最佳方法是什么?
XML文件示例:

<data>
  <currency>
    <system1>USD</system1>
    <system2>Dollar</system2>
  </currency>
  <currency>
    <system1>EUR</system1>
    <system2>Euro</system2>
  </currency>
</data>

最佳答案

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE xsl:stylesheet  [

  <!ENTITY data SYSTEM "mapping.xml">

]>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>

  <xsl:template match="node()" mode="mapping">
    <xsl:variable name="current" select="node()"/>
    <xsl:choose>

      <xsl:when test="name(.) = 'currency'">
        <xsl:variable name="data">
          &data;
        </xsl:variable>
        <xsl:value-of select="normalize-space(msxsl:node-set($data)/data/currency[system1 = $current]/system2)" />
      </xsl:when>

      <xsl:otherwise>
        <xsl:text>Unknown mapping node: </xsl:text>
        <xsl:value-of select="name(.)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

用途:
<xsl:apply-templates select="currency" mode="mapping"/>

关于xml - 使用XSLT 1.0映射外部数据的好方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49578593/

10-12 00:36
查看更多