Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

xslt - How to use two (or more) xml files in one xsl document?

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

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.

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>

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>

And the XSL file:

<?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:DATAMarqueedial_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:DATAMarqueedial_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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change

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

to

`<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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...