Binding to an element in another node (XSLT)

I have an XML document with the companies listed in it. I want to create a link with XSLT that contains a child of the <link>next node. Sorry if this is confusing. Here is an example of the XML I'm trying to get:

<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 need two links: "BACK" and "NEXT". While currently on mike.xml, I want BACK to reference "dano.xml" and the NEXT associated with "steve.xml" ... etc. And dynamically changed it on another page based on the nodes around it. I want to do this because I can add and change the list as I move, so I don’t want to manually reinstall everything.

? , XSLT, , , , ! !

+3
2

Dimitre, , document() XML " ".

, (dano.xml, mike.xml, steve.xml), ?

"mike.xml" . , , . <company> - . <compName> , <name> XML .

" " XML, "dano/mike/steve" XML, HTML-:

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>

:

<?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 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 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 Steve (steve.htm:)

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

:

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

XML-:

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

HTML "" "" :

<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>
+1

Source: https://habr.com/ru/post/1744965/


All Articles