简体   繁体   中英

How can I use XSLT to combine two XML?

i try code from How can I use XSLT to assemble an XML Hierarchy

a.xml:

    <Xml>
        <Classes>
            <Class Name="BIOLOGY101" ClassId="11"/>
            <Class Name="PHYSICS101" ClassId="13"/>
            <Class Name="CALCULUS101" ClassId="17"/>
            <Class Name="BIOLOGY101" ClassId="19"/>
        </Classes>
    </Xml>

b.xml :

<Xml>
    <Students>
        <Student Name="Bob Johnson" ClassId="11"/>
        <Student Name="Bob Johnson" ClassId="17"/>
        <Student Name="Bob Johnson" ClassId="19"/>
        <Student Name="Joe Jackson" ClassId="11"/>
        <Student Name="Joe Jackson" ClassId="13"/>
        <Student Name="Joe Jackson" ClassId="17"/>
        <Student Name="Rick Robertson" ClassId="13"/>
        <Student Name="Rick Robertson" ClassId="17"/>
        <Student Name="Rick Robertson" ClassId="19"/>
    </Students>
</Xml>

Stylesheet (merge.xsl):

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Class">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <!-- Apply <Student> elements from b.xml -->
      <xsl:apply-templates
        select="document('b.xml')/Xml/Students/Student
          [@ClassId = current()/@ClassId]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Student">
    <xsl:copy>
      <xsl:apply-templates select="@Name"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

hope output :

    <?xml version="1.0"?>
<Xml>
  <Classes>
    <Class Name="BIOLOGY101" ClassId="11">
      <Student Name="Bob Johnson"/>
      <Student Name="Joe Jackson"/>
    </Class>
    <Class Name="PHYSICS101" ClassId="13">
      <Student Name="Joe Jackson"/>
      <Student Name="Rick Robertson"/>
    </Class>
    <Class Name="CALCULUS101" ClassId="17">
      <Student Name="Bob Johnson"/>
      <Student Name="Joe Jackson"/>
      <Student Name="Rick Robertson"/>
    </Class>
    <Class Name="BIOLOGY101" ClassId="19">
      <Student Name="Bob Johnson"/>
      <Student Name="Rick Robertson"/>
    </Class>
  </Classes>
</Xml>

but when i launching with a.xml, i get the result is BLANK.. nothing showing anything in my browser mozilla.. (note : in header a.xml i write this code :

    <?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl"?>

somebody can me please ?? thanks..

It is working fine. Press F12 in order to view the result. For more information Please refer why browser (Firefox/Chrome) is showing blank result.

Attached screenshot (Developer tool)

在此处输入图片说明

Why XSLT is not working in browser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM