如何在一个 xsl 文档中使用两个(或更多)xml 文件? [英] How to use two (or more) xml files in one xsl document?

查看:22
本文介绍了如何在一个 xsl 文档中使用两个(或更多)xml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让同一个 xsl 文件处理两个(或更多)XML 文件.

I've been struggling for a while to get two (or more) XML files to be processed by the same xsl file.

我按照这篇文章中的步骤操作:包括一个 XMLXML/XSL 文件中的文件,但我无法让它工作.

I followed the steps in this post: Including an XML file in an XML/XSL file but I haven't been able to get this to work.

我似乎无法加载要处理的文件,没有错误.

I can't seem to get the file loaded to be processed, no error.

这是第一个 xm 文件 - Dial_Stats_MWB:

This is the first xm file - Dial_Stats_MWB:

<?xml version="1.0" encoding="utf-8"?>
<UK_Products_Pipeline>
  <LastFinishCode>
    <SiteName>UK</SiteName>
    <LastFinishCode>Agent Logout</LastFinishCode>
    <Numbers>1</Numbers>
  </LastFinishCode>
  <LastFinishCode>
    <SiteName>UK</SiteName>
    <LastFinishCode>Busy</LastFinishCode>
    <Numbers>1</Numbers>
  </LastFinishCode>
  <LastFinishCode>
    <SiteName>UK</SiteName>
    <LastFinishCode>BW Sale</LastFinishCode>
    <Numbers>1</Numbers>
  </LastFinishCode>
</UK_Products_Pipeline>

第二个文件 - Dial_Stats_UK:

The second file - Dial_Stats_UK:

<?xml version="1.0" encoding="utf-8"?>
<UK_Products_Pipeline>
  <LastFinishCode>
    <SiteName>MWB</SiteName>
    <LastFinishCode>Bearer Capability Not Presently Authorized (ISDN Cause Code 57)</LastFinishCode>
    <Numbers>1</Numbers>
  </LastFinishCode>
  <LastFinishCode>
    <SiteName>MWB</SiteName>
    <LastFinishCode>Confirmed Booking</LastFinishCode>
    <Numbers>1</Numbers>
  </LastFinishCode>
  <LastFinishCode>
    <SiteName>MWB</SiteName>
    <LastFinishCode>Lost</LastFinishCode>
    <Numbers>1</Numbers>
  </LastFinishCode>
</UK_Products_Pipeline>

和 XSL 文件:

<?xml version="1.0" encoding='utf-8'?>
<xsl:stylesheet xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
        <title> XSLT with XML included </title>
      </head>
      <body style="background-color:lightblue;color:green">
        <table cellSpacing="0" border="1" cellPadding="2">
          <!-- Set Variables -->
          <xsl:variable name="external">
            <xsl:copy-of select="document('D:\DATA\Marquee\dial_stats_UK.xml')/*"/>
          </xsl:variable>
          <!-- Process Data Start -->
          <xsl:for-each select="//UK_Products_Pipeline/LastFinishCode">
            <tr>
           <xsl:if test="SiteName ='MWB'">
                <td>
                  <xsl:value-of select="SiteName"/>
             </td>
                <td>
                  <xsl:value-of select="LastFinishCode"/>
                </td>
                <td>
                  <xsl:value-of select="Numbers"/>
                </td>
              </xsl:if>
            </tr>
          </xsl:for-each>
          <!-- Process File Data Start -->
            <xsl:call-template name="ExternalData">
            <xsl:with-param name="data" select="$external"/>
           </xsl:call-template>
        </table>
      </body>
    </html>
    </xsl:template>
  <xsl:template name="ExternalData">
    <xsl:param name="data"/>
    <xsl:variable name="external">
      <xsl:copy-of select="document('D:\DATA\Marquee\dial_stats_UK.xml')/*"/>
    </xsl:variable>
    <table cellSpacing="0" border="1" cellPadding="2" style="background-color:white;color:black">
        <tr>
          <td>
            I do see this.
          </td>
        </tr>
        <!-- Process External Data -->
        <xsl:for-each select="//UK_Products_Pipeline/LastFinishCode">
          <tr>
            <td>
              <xsl:value-of select="SiteName"/>
            </td>
          </tr>
          <tr>
          <xsl:if test="SiteName ='UK'">
            <td>
              <xsl:value-of select="SiteName"/>
            </td>
            <td>
              <xsl:value-of select="LastFinishCode"/>
            </td>
            <td>
              <xsl:value-of select="Numbers"/>
            </td>
          </xsl:if>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

当处理发生时,将再次处理同一个文件,而不是第二个文件.

When the processing takes place the same file is processed again not the second file.

我不知道你是否可以就我在这里做错的地方给我任何建议?

I don't know whether or not you can give me any suggestions on what I do wrong here please?

推荐答案

更改

`<xsl:for-each select="//UK_Products_Pipeline/LastFinishCode">` 

`<xsl:for-each select="document('file:///D:/DATA/Marquee/dial_stats_UK.xml')/UK_Products_Pipeline/LastFinishCode">`

在要处理来自第二个输入文件的数据的模板中.

in the template where you want to process data from the second input file.

尽管更简洁的方法是为要处理的第二个文件中的节点编写具有模式的匹配模板.那么你就可以这样做:

Although a cleaner approach is to write matching templates with a mode for the nodes from the second file you want be processed. Then you just would do:

`<xsl:apply-templates select="document('file:///D:/DATA/Marquee/dial_stats_UK.xml')/UK_Products_Pipeline" mode="my-mode"/>` 

您的模板将输出您想要的表格.

and your templates for that mode would output the table you want.

这篇关于如何在一个 xsl 文档中使用两个(或更多)xml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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