簡體   English   中英

XSLT-比較當前節點與先前的同級

[英]XSLT - comparing current node with previous sibling

我有一個輸入xml看起來像這樣:

<vtext>
    <myTag>Title</myTag>
</vtext>
<vtext>
    <myTag> </myTag>
</vtext>
<vtext>
    <myTag> </myTag>
</vtext>
<vtext>
    <myTag>Some text here maybe</myTag>
</vtext>
<vtext>
    <myTag> </myTag>
</vtext>
<vtext>
    <myTag> </myTag>
</vtext>
<vtext>
    <myTag> </myTag>
</vtext>
<vtext>
    <myTag>Other text...</myTag>
</vtext>

<vtext>節點始終包含單個<myTag>子節點,該子節點可能為空。 (在此示例中, ,但也可以是<myTag\\>類的東西

而我想要實現的是擁有一個看起來像這樣的輸出HTML:

Title<br>
<br>
Some text here maybe<br>
<br>
Other text...

基本上,我想用僅一個<br>標簽將多個空的<myTag>節點彼此替換。 為此,我正在使用xsl轉換,該轉換需要一個額外的條件,該條件我目前無法解決(無法弄清楚)...

我現在所擁有的是:

<xsl:for-each select="myTag">
    <xsl:choose>
        <xsl:when
            test="normalize-space(current()) = '' and **SOME CONDITION INVOLVING preceding-sibling MAYBE??**> 
        <br />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="current()" />
        <br />
    </xsl:otherwise>
</xsl:choose>

有什么想法需要額外的條件嗎?

謝謝

由於myTagmyTag的子級, vtext您可能需要更改xsl:for-each以選擇vtext元素而不是myTag 此外,您還可以添加一個條件以僅選擇myTag非空的條件,或前一個非空的條件。

<xsl:for-each select="vtext[normalize-space(myTag) or normalize-space(preceding-sibling::vtext[1]/myTag)]">

因此,您要同時捕獲兩個條件。

試試這個XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"  />

    <xsl:template match="/*">
        <xsl:for-each select="vtext[normalize-space(myTag) or normalize-space(preceding-sibling::vtext[1]/myTag)]">
            <xsl:value-of select="normalize-space(myTag)" />
            <br />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

也可以這樣完成:

<xsl:template match="vtext">
    <xsl:for-each select="myTag">
        <xsl:choose>
            <!-- If the current 'myTag' is empty and the previously one is not append an empty line -->
            <xsl:when 
               test="string-length(normalize-space(current())) = 0 
                         and not(string-length(normalize-space(../preceding-sibling::vtext[1]/myTag)) = 0)">
               <br />
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <!-- If the current 'myTag' is not empty, add it to the existing document -->
                    <xsl:when test="not(string-length(normalize-space(current())) = 0)">  
                        <xsl:value-of select="current()" />
                        <br />
                    </xsl:when>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM