开发者

Linking to an item in another node (XSLT)

I have an XML document with companies listed in it. I want to create a link with XSLT that contains the <link> child of the next node. Sorry if this is confusing..h开发者_JAVA技巧ere is some sample XML of what i'm trying to obtain:

<portfolio>

<company>
<name>Dano Industries</name>
<link>dano.xml</link>
</company>

<company>
<name>Mike and Co.</name>
<link>mike.xml</link>
</company>

<company>
<name>Steve Inc.</name>
<link>steve.xml</link>
</company>

</portfolio>

I want two links, "BACK" and "NEXT". While currently on mike.xml, I want BACK to link to "dano.xml" and NEXT linked to "steve.xml"...etc..and have it dynamically change when on a different page based on the nodes around it. I want to do this because I may add and change the list as I go along, so I don't want to have to manually re-link everything.

How can I obtain this? Sorry I am new to XSLT, so please explain with solution if possible! Thanks in advance!


Based on your comments to Dimitre, I think what you're going to want to do is use the document() function to access your "master list" XML file.

What you are actually running the stylesheet on is the individual fragments (dano.xml, mike.xml, steve.xml), right?

I'll use "mike.xml" for an example. I don't know what the fragments look like so I had to make one up. You will need to be able to identify the correct <company> in the master list based on something in the fragment. In my example, the fragment has a <compName> element with the same value as the <name> element in the corresponding company in the master list XML.

Here is what the "master list" XML, "dano/mike/steve" XML, the stylesheet, and the resulting HTML look like:

master_list.xml:

<?xml version="1.0" encoding="UTF-8"?>
<portfolio>

   <company>
      <name>Dano Industries</name>
      <link>dano.xml</link>
   </company>

   <company>
      <name>Mike and Co.</name>
      <link>mike.xml</link>
   </company>

   <company>
      <name>Steve Inc.</name>
      <link>steve.xml</link>
   </company>

</portfolio>

dano.xml

<?xml version="1.0" encoding="UTF-8"?>
<fragment>
   <compName>Dano Industries</compName>
   <compInfo>Some info about Dano Industries</compInfo>
</fragment>

mike.xml:

<?xml version="1.0" encoding="UTF-8"?>
<fragment>
   <compName>Mike and Co.</compName>
   <compInfo>Some info about Mike and Co.</compInfo>
</fragment>

steve.xml

<?xml version="1.0" encoding="UTF-8"?>
<fragment>
   <compName>Steve Inc.</compName>
   <compInfo>Some info about Steve Inc.</compInfo>
</fragment>

stylesheet:

<?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" indent="yes"/>
   <xsl:strip-space elements="*"/>

   <xsl:template match="fragment">
      <xsl:variable name="name" select="compName"/>
      <xsl:variable name="previous-file">
         <xsl:value-of select="document('master_list.xml')/portfolio/company[name=$name]/preceding-sibling::company[1]/link"/>
      </xsl:variable>
      <xsl:variable name="next-file">
         <xsl:value-of select="document('master_list.xml')/portfolio/company[name=$name]/following-sibling::company[1]/link"/>
      </xsl:variable>
      <html>
         <xsl:apply-templates/>
         <p>
            <xsl:if test="not($previous-file='')">
               <a href="{$previous-file}">Back</a>
            </xsl:if>
            <xsl:if test="not($previous-file='') and not($next-file='')">
               <xsl:text>&#xA0;|&#xA0;</xsl:text>
            </xsl:if>
            <xsl:if test="not($next-file='')">
               <a href="{$next-file}">Next</a>  
            </xsl:if>
         </p>
      </html>
   </xsl:template>

   <xsl:template match="compName">
      <h1><xsl:apply-templates/></h1>
   </xsl:template>

   <xsl:template match="compInfo">
      <p><xsl:apply-templates/></p>
   </xsl:template>

</xsl:stylesheet>

HTML for Dano (dano.htm:)

<html>
   <h1>Dano Industries</h1>
   <p>Some info about Dano Industries</p>
   <p><a href="mike.xml">Next</a></p>
</html>

HTML for Mike (mike.htm:)

<html>
   <h1>Mike and Co.</h1>
   <p>Some info about Mike and Co.</p>
   <p><a href="dano.xml">Back</a>&nbsp;|&nbsp;<a href="steve.xml">Next</a></p>
</html>

HTML for Steve (steve.htm:)

<html>
   <h1>Steve Inc.</h1>
   <p>Some info about Steve Inc.</p>
   <p><a href="mike.xml">Back</a></p>
</html>


This transformation:

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

 <xsl:template match="/*">
  <html>
    <table border="1">
      <xsl:apply-templates/>
    </table>
  </html>
 </xsl:template>

 <xsl:template match="company">
   <xsl:variable name="vPrevious"
     select="preceding-sibling::company[1]/link"/>

   <xsl:variable name="vNext"
     select="following-sibling::company[1]/link"/>
   <tr>
     <td>
       <a href="{link}"><xsl:value-of select="name"/></a>
     </td>
     <td>
      &#xA0;
      <xsl:if test="$vPrevious">
       <a href="{$vPrevious}">Back</a>
      </xsl:if>
     </td>
     <td>
      &#xA0;
      <xsl:if test="$vNext">
       <a href="{$vNext}">Next</a>
      </xsl:if>
     </td>
   </tr>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<portfolio>
    <company>
        <name>Dano Industries</name>
        <link>dano.xml</link>
    </company>
    <company>
        <name>Mike and Co.</name>
        <link>mike.xml</link>
    </company>
    <company>
        <name>Steve Inc.</name>
        <link>steve.xml</link>
    </company>
</portfolio>

produces the desired HTML table with "Back" and "Next" links:

<html>
    <table border="1">
        <tr>
            <td>
                <a href="dano.xml">Dano Industries</a>
            </td>
            <td>      </td>
            <td>
                <a href="mike.xml">Next</a>
            </td>
        </tr>
        <tr>
            <td>
                <a href="mike.xml">Mike and Co.</a>
            </td>
            <td>
                <a href="dano.xml">Back</a>
            </td>
            <td>
                <a href="steve.xml">Next</a>
            </td>
        </tr>
        <tr>
            <td>
                <a href="steve.xml">Steve Inc.</a>
            </td>
            <td>
                <a href="mike.xml">Back</a>
            </td>
            <td>      </td>
        </tr>
    </table>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜