Including an XML file in an XML/XSL file

2019-01-10 23:37发布

So currently I'm doing some XML-> XSLT-> (HTML5/CSS3) work. Right now I have a menu.xml file, and I'd like to include it in either the XSL file or the XML page. I've done lots of searching, but I'm unable to find a straightforward answer.

So, how do I include an XML file in to another XML file or in to a XSL file?

Edit: By include, I mean referencing/loading it from another file, not copy and pasting it or simply embedding it.

标签: xml xslt
5条回答
Melony?
2楼-- · 2019-01-10 23:40

I was unable to get the code to work that people provided. However I did get a solution after trying several things. Name an xml file clGroup.xml (first two letters don't matter, but I am finding "Group" is required. This xml file will reference the xml you want to parse.

clGroup xml:

 <?xml version='1.0' encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="calendarGroup.xslt" ?>
<groups>
  <groupRef>caGroup.xml</groupRef>
</groups>

Now name a file caGroup.xml and put in the following code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<topics xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <topic>
        <topicstopic>Calendar</topicstopic>
        <topicsubmitter>Calendar</topicsubmitter>
        <creatoremail>joeshowfakeaccount@gmail.com</creatoremail>
    </topic>
    <topic>
        <name>Week1</name>
        <sunday/>
        <monday/>
        <tuesday>Test01 Young Men Weekly</tuesday>
        <wednesday>02 Elders Home Teaching Message Birthday</wednesday>
        <thursday>03 </thursday>
        <friday>04 </friday>
        <saturday>05  #Young Men Weekly</saturday>
    </topic>
    <topic>
        <name>Week2</name>
        <sunday>06 Fast Sunday</sunday>
        <monday>07 FHE</monday>
        <tuesday>08 Young Men Tonight #Young Men Weekly going to Bishops storehouse 6PM to 7PM Young Men Weekly</tuesday>
        <wednesday>09 </wednesday>
        <thursday>10 Scout Round Table at 7 PM </thursday>
        <friday>11 </friday>
        <saturday>12 </saturday>
    </topic>
  </topics>

Now here is my XSLT file calendarGroup.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" 
            version="1.0" 
            encoding="UTF-8" 
            indent="yes"/>

<xsl:template match="groups/groupRef">
  <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>Table</title>
      <meta charset="utf-8" />
    </head>
    <body>
      <table border="1">
        <tr>
          <th>Sunday</th>
          <th>Monday</th>
          <th>Tuesday</th>
          <th>Wednesday</th>
          <th>Thursday</th>
          <th>Friday</th>
          <th>Saturday</th>
        </tr>         
        <xsl:for-each select="document(.)//topic">
          <xsl:if test="string(name) != ''">
          <tr>
            <td>
              <xsl:value-of select="sunday"/>
            </td>
            <td>
              <xsl:value-of select="monday"/>
            </td>
            <td>
              <xsl:value-of select="tuesday"/>
            </td>
            <td>
              <xsl:value-of select="wednesday"/>
            </td>
            <td>
              <xsl:value-of select="thursday"/>
            </td>
            <td>
              <xsl:value-of select="friday"/>
            </td>
            <td>
              <xsl:value-of select="saturday"/>
            </td>
          </tr>
          </xsl:if>
        </xsl:for-each>
      </table>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>

The benefit of setting up my code this way is I can easily create the xml from an excel file and not half to put in a declaration statement in the newly created file. You do need to have the format "xxGroup" for this to work together. You can add more groups to the clGroup file and they will parse in the XSLT allowing you to include more xml in the same XSLT parser. Make sure they are all in the same directory.

查看更多
3楼-- · 2019-01-10 23:48

If simply embedding the file inside the XML file is not a solution then adding a url field that the interpreting program reads should be added- you're basically asking for the equivalent of an include(menu.xml) or require(xml), but in an XML file.

So since you are writing the program that is intepretting the file you can add an <externalMenuTagThatYouDecideToAddToTheBaseXmlFile> tag or whatever you want to call it that you will read and insert menu.xml's root note the place of the <externalMenuTagThatYouDecideToAddToTheBaseXmlFile>.

Good luck and may your programs always compile.

查看更多
Root(大扎)
4楼-- · 2019-01-10 23:55

I. Here is how any XML document or fragment can be embedded in an XSLT stylesheet and used during the transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:menu>
   <menu>
     <choice>A</choice>
     <choice>B</choice>
     <choice>C</choice>
   </menu>
 </my:menu>

 <xsl:template match="/">
  <xsl:copy-of select="document('')/*/my:menu/*"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document (not used in this example), the wanted result (just copying the XML) is produced:

<menu xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my">
   <choice>A</choice>
   <choice>B</choice>
   <choice>C</choice>
</menu>

Remember: Any XML can be embedded into an XSLT stylesheet, provided it is wrapped into a namespaced element (the namespace not the XSLT namespace) and this wrapping element is at the global level (a child of the <xsl:stylesheet> (top) element).

II. Accessing the XML menu file that resides in a separate XML file:

To do this we have to change only slightly the previous example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>


 <xsl:template match="/">
  <xsl:copy-of select="document('menu.XML')/*"/>
 </xsl:template>
</xsl:stylesheet>

If the menu XML file is in the 'menu.XML' file (in the same directory as the XSLT stylesheet file, then this transformation produces exactly the same result as the previous:

<menu>
   <choice>A</choice>
   <choice>B</choice>
   <choice>C</choice>
</menu>

Do note: In both cases we are using the standard XSLT function document()

Typically, one defines a global-level variable, whose value is the result of calling the document() function. Then this variable and its contents is accessed via XPath expressions during the transformation.

查看更多
爷的心禁止访问
5楼-- · 2019-01-10 23:56

So, how do I include an XML file in to another XML file or in to a XSL file?

You can use an external entity to reference the menu.xml file and include the content into either an XML file or the XSLT (or both).

By include, I mean referencing/loading it from another file, not copy and pasting it or simply embedding it.

By using external entities, you can reference/load the menu.xml content from external files and do not have to duplicate the XML content.

For instance, if you wanted the menu.xml content included in your XSLT, you would declare the external entity in your XSLT like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet [
     <!ENTITY menu SYSTEM "./menu.xml">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

and could then reference it like you would any other entity:

&menu;

When the XSLT parsed, the entity reference will be expanded and the XML content of the menu.xml will be included as part of the XSLT document as if you had copy/pasted into the spot where the entity reference was.

查看更多
疯言疯语
6楼-- · 2019-01-10 23:56

I have a blog post on this. You use document() to get the other XML and pass around the content using parameters.

查看更多
登录 后发表回答