2014-10-21 2 views
1
<?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" omit-xml-declaration="yes" encoding="UTF-8"/> 
<xsl:param name="contextPath">CONTEXT_PATH/</xsl:param> 
<xsl:include href="xhtml.xsl"/> 
<xsl:variable name="site"><xsl:value-of select="/leftnavigation/tree/path/node[2]/@name"/></xsl:variable> 

     <xsl:template match="leftnavigation"> 
      <xsl:element name="h1"> 
       <xsl:attribute name="id">banner</xsl:attribute> 

         <xsl:apply-templates select="bannerleftimage" /> 
         <xsl:apply-templates select="bannermiddleimage" /> 
         <xsl:if test="$site='Families'"> 
          <xsl:apply-templates select="bannerrightimage" /> 

          <!-- Just to output the value for dev purposes --> 
          <xsl:text><xsl:value-of select="$site"/></xsl:text> 
         </xsl:if> 

      </xsl:element> 
      <xsl:element name="div"> 
       <xsl:attribute name="id">left_col</xsl:attribute> 
       <xsl:apply-templates select="sectionlist" /> 
      </xsl:element> 
      </xsl:template> 

      <xsl:template match="bannerleftimage">   
      <xsl:variable name="currentsrc" select="@filename"/> 
      <xsl:variable name="origwidth"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@width"/> 
      </xsl:variable> 
      <xsl:variable name="origheight"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@height"/> 
      </xsl:variable> 
      <xsl:variable name="ratio"> 
       <xsl:value-of select="$origwidth div $origheight"/> 
      </xsl:variable> 
      <xsl:variable name="origalt"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@alt"/> 
      </xsl:variable> 
      <xsl:variable name="origtitle"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@title"/> 
      </xsl:variable> 
      <xsl:element name="a"> 
        <xsl:attribute name="href"><xsl:value-of select="$contextPath"/>/home.html</xsl:attribute> 
       <xsl:element name="img"> 
        <xsl:attribute name="src"><xsl:value-of select="$contextPath"/>/images/banners/<xsl:value-of select="@filename"/></xsl:attribute> 
        <xsl:attribute name="width">365</xsl:attribute> 
        <xsl:attribute name="height">100</xsl:attribute> 
        <xsl:attribute name="title"><xsl:value-of select="$origtitle"/></xsl:attribute> 
        <xsl:attribute name="alt"><xsl:value-of select="$origalt"/></xsl:attribute> 
       </xsl:element> 
      </xsl:element> 
     </xsl:template> 

     <xsl:template match="bannermiddleimage">    
      <xsl:variable name="currentsrc" select="@filename"/> 
      <xsl:variable name="origwidth"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@width"/> 
      </xsl:variable> 
      <xsl:variable name="origheight"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@height"/> 
      </xsl:variable> 
      <xsl:variable name="ratio"> 
       <xsl:value-of select="$origwidth div $origheight"/> 
      </xsl:variable> 
      <xsl:variable name="origalt"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@alt"/> 
      </xsl:variable> 
      <xsl:variable name="origtitle"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@title"/> 
      </xsl:variable> 
      <xsl:element name="a"> 
        <xsl:attribute name="href"><xsl:value-of select="$contextPath"/>/home.html</xsl:attribute> 
       <xsl:element name="img"> 
        <xsl:attribute name="id">banmiddle</xsl:attribute> 
        <xsl:attribute name="src"><xsl:value-of select="$contextPath"/>/images/banners/<xsl:value-of select="@filename"/></xsl:attribute> 
        <xsl:attribute name="width">1</xsl:attribute> 
        <xsl:attribute name="height">100</xsl:attribute> 
        <xsl:attribute name="title"><xsl:value-of select="$origtitle"/></xsl:attribute> 
        <xsl:attribute name="alt"><xsl:value-of select="$origalt"/></xsl:attribute> 
       </xsl:element> 
      </xsl:element> 
     </xsl:template> 

     <xsl:template match="bannerrightimage"> 
      <xsl:variable name="currentsrc" select="@filename"/> 
      <xsl:variable name="origwidth"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@width"/> 
      </xsl:variable> 
      <xsl:variable name="origheight"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@height"/> 
      </xsl:variable> 
      <xsl:variable name="ratio"> 
       <xsl:value-of select="$origwidth div $origheight"/> 
      </xsl:variable> 
      <xsl:variable name="origalt"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@alt"/> 
      </xsl:variable> 
      <xsl:variable name="origtitle"> 
       <xsl:value-of select="//gallery/image[@filename=$currentsrc]/@title"/> 
      </xsl:variable> 
      <xsl:element name="div"> 
       <xsl:attribute name="id">bannerright</xsl:attribute>    
       <xsl:element name="div"> 
        <xsl:attribute name="id">talktoexpert</xsl:attribute> 
        <xsl:attribute name="title">Talk to a specialist consultant</xsl:attribute> 
        Talk to a specialist consultant 
       </xsl:element> 
       <xsl:element name="div"> 
        <xsl:attribute name="id">bannertel</xsl:attribute> 
        <xsl:attribute name="title"><xsl:value-of select="$origtitle"/></xsl:attribute> 
        <xsl:value-of select="../telephone"/> 
       </xsl:element>  
       <xsl:element name="div"> 
         <xsl:attribute name="id">bannertelint</xsl:attribute> 
         <xsl:attribute name="title"><xsl:value-of select="$origtitle"/></xsl:attribute> 
         <xsl:value-of select="../telephoneint"/> 
       </xsl:element> 
      </xsl:element> 
     </xsl:template> 


     <xsl:template match="sectionlist"> 

     <xsl:element name="ul"> 
      <xsl:attribute name="id">side_menu</xsl:attribute> 
      <xsl:for-each select="section"> 
       <xsl:element name="li"> 
        <xsl:if test="@cssclass"> 
         <xsl:attribute name="class"><xsl:value-of select="@cssclass"/></xsl:attribute> 
        </xsl:if> 
        <xsl:if test="@cssid"> 
         <xsl:attribute name="id"><xsl:value-of select="@cssid"/></xsl:attribute> 
        </xsl:if> 
        <xsl:apply-templates select="xhtml"/> 
       </xsl:element> 
      </xsl:for-each> 

     </xsl:element> 

    </xsl:stylesheet> 

То, что я пытаюсь достичь:
Я пытаюсь установить состояние вокруг шаблона (bannerrightimage) не появляется для конкретной марки (в вашем случае String) называется 'Families'. @name обычно возвращает три строки, Families, Children, Adults. И он работает в моих других файлах xsl.переменная XSL не передавая значения в

Проблема, с которой я сталкиваюсь, заключается в том, что переменная $site не передает никаких значений. но если я что-то вроде:

<xsl:variable name="site"><xsl:text>ABC123</xsl:text> 

$site делает выходной ABC123. Как я уже говорил раньше, если я в home.xsl я бы свою переменную как:

home.xsl

<xsl:variable name="site"><xsl:value-of select="/home/tree/path/node[2]/@name"/> 

и если это country.xsl я установил бы свою переменную как:

<xsl:variable name="site"><xsl:value-of select="/country/tree/path/node[2]/@name"/> 

home.xsl и country.xsl работа прекрасно, но когда я реализую один и тот же стиль для моего leftnavigation.xsl$site не возвращает никакого значения. даже не пустое место ...

+1

Поскольку похоже, что вы ничего не выбираете в своем исходном XML, это поможет, если вы также продемонстрируете часть XML, с которой работаете. Кроме того, ваши фрагменты кода для '$ site' имели бы больше смысла, если бы вы включили конечные теги. В-третьих, ' ABC123' может быть '' и ' 'может быть' ' –

+0

@TonyGraham Я редактировал сообщение, чтобы источник мог включать все, что я вызываю и использую. Кроме того, я пробовал все эти стили с точки зрения того, что он похож на '', как вы упомянули. Но после попытки, как 5000 раз, я закончил эту нелепую конвенцию, и я оставил ее в этом состоянии (пока). – Arty

+2

Извините, я хотел бы видеть ваш хорошо сформированный исходный XML-код, который начинается с '' и включает в себя до второго '', поэтому мы можем видеть, насколько ваш XPath соответствует вашей структуре. –

ответ

1

ОГРОМНОЕ СПАСИБО @Tony Graham !! Когда вы упомянули о структуре соответствия XPath, я понял, что не проверял файл leftnavigation.xml. И я понял, что во всех других xml-файлах у них есть раздел «COMMON» в их xsd s, который включает в себя узлы, которых я искал. Тем не менее, в моем файле leftnavigation.xsd, у него нет этого раздела, поэтому, когда я искал узлы, которые я искал, назовите [NULL].

Так вот что я узнаю, файл home-example.xml ищет noNamespacesSchemaLocation="home.xsd"

Так в XML-файл в разделе Я искал это раздел узлы, которые ниже.

дом-example.xml:

<path> 
     <node name="root" url="" type="ROOT" mainimage=""/> 
     <node name="Wildlife" type="SITE" mainimage=""/> 
     <node name="2006" type="VERSION" mainimage=""/> 
     <node name="homepages" type="SECTION" mainimage=""/> 
     <node name="First Home Page" url="/home/home.html" mainimage=""/> 
    </path> 

Так в XSD, есть Commons раздел, который выглядит следующим образом:
home.xsd:
======= ========= Общие ==================================

</xs:documentation> 
</xs:annotation> 
<xs:element name="node"> 
    <xs:complexType> 
     <xs:sequence minOccurs="0" maxOccurs="1"> 
      <xs:element ref="xhtml"/> 
     </xs:sequence> 
     <xs:attribute name="name" type="xs:string"/> 
     <xs:attribute name="url" type="xs:string"/> 
     <xs:attribute name="abstract" type="xs:string"/> 
     <xs:attribute name="mainimage" type="xs:string"/> 
     <xs:attribute name="type" type="xs:string"/> 
     <xs:attribute name="metadata" type="xs:string"/> 
    </xs:complexType> 
</xs:element> 
<xs:element name="sectionimg"> 
    <xs:complexType> 
     <xs:attribute name="src" type="xs:string"/> 
     <xs:attribute name="alt" type="xs:string"/> 
     <xs:attribute name="width" type="xs:string"/> 
     <xs:attribute name="height" type="xs:string"/> 
     <xs:attribute name="id" type="xs:string"/> 
     <xs:attribute name="class" type="xs:string"/>   
    </xs:complexType> 
</xs:element> 
<xs:complexType name="treeType"> 
    <xs:sequence> 
     <xs:element name="parent"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element ref="node" minOccurs="0"/> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="children"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element ref="node" minOccurs="0" maxOccurs="100"/> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="siblings"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element ref="node" minOccurs="0" maxOccurs="100"/> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="path"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element ref="node" minOccurs="0" maxOccurs="100"/> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
    </xs:sequence> 
</xs:complexType> 

Однако в моем файле leftnavigation-example.xml и в моем leftnavigation.xsd, у меня не было как в разделе общих в XSD и раздел узлов в моем XML поэтому, когда я попытался назвать переменную:

<xsl:variable name="site"><xsl:value-of select="/leftnavigation/tree/path/node[2]/@name"/></xsl:variable> 

я не получал ничего, потому что вернулся стоимости выбранного пути не существует.

Еще раз спасибо @tony graham за то, что указали мне в правильном направлении ....

Смежные вопросы