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



我正在尝试遍历一个节点,这在它不是问题。我想检查 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>

解决方案很简单

只需替换:

 <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) ]
         )
      ]"
 />

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

相关内容

  • 没有找到相关文章

最新更新