XSLT:在条件满足至少一次时循环并设置变量

时间:2012-05-16 07:42:54

标签: variables loops conditional xslt-2.0 setting

我正在尝试遍历一个节点,该节点在itselt中不是问题。我想检查selectedLetter是否与节点的第一个字母匹配,这是按预期工作的(参见下面的代码)。如果条件“”在循环期间至少没有遇到一次,那么我想提出一些替代文本的困难,就像“对不起,没有文件以那封信开头”。我正在考虑在条件中设置变量即标志,稍后检查是否已设置此变量,但是从我在此处读到的内容,变量的范围是受限制的并且仅在循环内可用?任何人都可以提供任何帮助吗?

干杯, 塔夫

<xsl:param name="selectedLetter" select="''"/>
<xsl:key name="docLetter" match="docs" use="substring(text()),1,1)"/>

                <xsl:for-each select="//ad-documents/item">

                    <xsl:sort select="documentName"/>
                    <xsl:variable name="firstLetter" select="upper-case(substring(documentName, 1, 1))"/>

                    <xsl:choose>

                        <xsl:when test="$firstLetter = $selectedLetter">

                            <div class="doc-date">
                                <xsl:if test="upload-date/day &lt; 10">0</xsl:if>
                                <xsl:value-of select="upload-date/day"/>
                                <xsl:text>.</xsl:text>
                                <xsl:if test="upload-date/month &lt; 10">0</xsl:if>
                                <xsl:value-of select="upload-date/month"/>
                                <xsl:text>.</xsl:text>
                                <xsl:value-of select="upload-date/year"/>
                            </div>

                            <div class="doc-link">
                                <a>
                                    <xsl:attribute name="href">
                                        <xsl:choose>
                                            <xsl:when test="isEditableFormDoc/box = 'true'">
                                                #
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:text>/img/ejbfile/</xsl:text>
                                                <xsl:value-of select="documents/document/@documentName"/>
                                                <xsl:text>?id=</xsl:text>
                                                <xsl:value-of select="documents/document/@src"/>
                                            </xsl:otherwise>
                                        </xsl:choose>      
                                    </xsl:attribute>
                                    <xsl:if test="isEditableFormDoc/box = 'true'">
                                        <xsl:attribute name="target">
                                            <xsl:text>_blank</xsl:text>
                                        </xsl:attribute>
                                    </xsl:if>
                                    <xsl:value-of select="documentName"/>
                                </a>
                            </div>

                            <div class="doc-short-desc">
                                <xsl:apply-templates select="document-short-desc" mode="format"/>
                            </div>

                            <!--<xsl:apply-templates select="//ad-documents/item" mode="filteredDocuments"/>-->
                        </xsl:when>
                    </xsl:choose>

              </xsl:for-each>

编辑:

XML示例

<ad-documents name="ad-documents" label="Dokumente (limitiert auf 20 pro Seite)">
  <item description="Doc%203" id="1" timestamp="1328525592205">
    <documentName name="documentName" description="Doc%203">Doc 3</documentName>
    <optionalLetter name="optionalLetter" description="c">c</optionalLetter>
    <document-short-desc name="document-short-desc" description="">
      <p>yxf</p>
    </document-short-desc>
    <upload-date name="upload-date" description="">
      <day>24</day>
      <month>2</month>
      <year>2012</year>
    </upload-date>
    <isEditableFormDoc name="isEditableFormDoc" description="">
      <box/>
    </isEditableFormDoc>
    <documents name="documents" description=""/>
  </item>
  <item description="Doc%204" id="2" timestamp="1328525624889">
    <documentName name="documentName" description="Doc%204">Doc 4</documentName>
    <optionalLetter name="optionalLetter" description="z%2Ci%2Cg">z,i,g</optionalLetter>
    <document-short-desc name="document-short-desc" description="">
      <p>asff</p>
    </document-short-desc>
    <upload-date name="upload-date" description="">
      <day>25</day>
      <month>2</month>
      <year>2012</year>
    </upload-date>
    <isEditableFormDoc name="isEditableFormDoc" description="">
      <box/>
    </isEditableFormDoc>
    <documents name="documents" description=""/>
  </item>
  <item description="Doc%201" id="1" timestamp="1328523551639">
    <documentName name="documentName" description="Doc%201">Doc 1</documentName>
    <optionalLetter name="optionalLetter" description="b%2Cc">b,c</optionalLetter>
    <document-short-desc name="document-short-desc" description="">
      <p>Short Desc 1</p>
    </document-short-desc>
    <upload-date name="upload-date" description="">
      <day>9</day>
      <month>2</month>
      <year>2012</year>
    </upload-date>
    <isEditableFormDoc name="isEditableFormDoc" description="">
      <box/>
    </isEditableFormDoc>
    <documents name="documents" description=""/>
  </item>
</ad-documents>

2 个答案:

答案 0 :(得分:0)

解决方案很简单

只需替换:

 <xsl:for-each select="//ad-documents/item">

 <xsl:for-each select=
    "//ad-documents/item
            [$selectedLetter eq upper-case(substring(documentName, 1, 1) ]">

您可以在此xsl:for-each

之后添加
<xsl:sequence select=
 "'YourErrorMessage'
     [not(//ad-documents/item
                [$selectedLetter eq upper-case(substring(documentName, 1, 1) ]

         )
      ]"
 />

答案 1 :(得分:0)

感谢Dimitre,我设法用count()

解决了这个问题
<xsl:variable name="foundDocs" select="count(//ad-documents/item[$selectedLetter eq upper-case(substring(documentName, 1, 1))])"/>
                <xsl:if test="$foundDocs = 0">
                    <div class="no-documents">
                        <xsl:text>Leider keine Dokumente gefunden</xsl:text>
                    </div>
                </xsl:if>