2017-09-20 20 views
0

如果文本包含<vehicleType><color>中的任何單詞,我正試圖獲取<line>中的文本。我還需要計算<description>中包含的<vehicleType><color>中有多少個單詞。是否有可能使用XSL(版本= 1.0)?XSL:獲取單獨節點中包含任何單詞的文本值

輸出應看起來像:

Vehicle type (4) 
This vehicle is a white <font color="red">compact car</font>. 
This vehicle is a orange <font color="red">minivan</font>. 
This vehicle is a red <font color="red">sedan</font>. 
This vehicle is a yellow <font color="red">truck</font>. 

Color (4) 
This vehicle is a <font color="red">white</font> compact car. 
This vehicle is a <font color="red">red</font> sedan. 
This vehicle is a <font color="red">yellow</font> truck. 
This vehicle is a <font color="red">yellow</font> crossover. 

這裏是簡化的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="test9.xsl"?> 
<items> 
    <vehicleTypes> 
    <vehicleType>truck</vehicleType> 
    <vehicleType>compace</vehicleType> 
    <vehicleType>car</vehicleType> 
    <vehicleType>sedan</vehicleType> 
    <vehicleType>minivan</vehicleType> 
    </vehicleTypes> 
    <colors> 
    <color>red</color> 
    <color>yellow</color> 
    <color>blue</color> 
    <color>white</color> 
    <color>purple</color> 
    <color>gold</color> 
    <color>silver</color> 
    </colors> 
    <item> 
     <description> 
      <p> 
       <line> 
        This vehicle is a white compact car. 
       </line> 
       <line> 
        This vehicle is a orange minivan. 
       </line> 
       <line> 
        This vehicle is a red sedan. 
       </line> 
       <line> 
        This vehicle is a yellow truck. 
       </line> 
       <line> 
        This vehicle is a yellow crossover. 
       </line> 
      </p> 
     </description> 
    </item> 
</items> 

回答

0

爲了測試任何在「vehicleType」或「色彩」的逗號分隔的值是否發生在當前元素作爲空格或標點符號分隔的單詞或短語,我會寫這樣的東西(未測試):

<xsl:variable name="needles.vT" as="xs:string" 
    select="tokenize(/items/vehicleType,',')"/> 
<xsl:variable name="needles.c" as="xs:string" 
    select="tokenize(/items/color,',')"/> 
<xsl:variable name="haystack" as="xs:string" 
    select="concat(' ', 
        normalize-space(
         translate(., 
         ' .,;:!?()-=+', 
         '   '), 
        ' ')"/> 
<xsl:if test="some $needle in ($needles.vT, $needles.c) 
       satisfies contains($haystack, 
          concat(' ', $needle, ' ')"> 
    ... code for magic-word-found case ... 
</xsl:if> 

可能有更優雅的方法來做到這一點。

要獲得vehicleType和顏色針在草堆數的計數,我會使用類似

count($needles.vT[contains($haystack, concat(' ', ., ' '))]) 
count($needles.c[contains($haystack, concat(' ', ., ' '))]) 

在XSLT 1.0,我會寫,通過列表的作品遞歸模板術語,剝離第一個術語,如果匹配,則向累加器添加一個,並重復出現在術語列表的其餘部分。一般形式是

<xsl:template name="countHits"> 
    <xsl:param name="needles"/><!--* terms to seek *--> 
    <xsl:param name="haystack"/><!--* where to seek them *--> 
    <xsl:param name="accumulator" select="0"/> 
    <!--* number of hits so far *--> 

    <xsl:variable name="needle"> 
    <xsl:choose> 
     <xsl:when test="contains($needles, ',')"> 
     <xsl:value-of select="substring-before($needles, ',')"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$needles"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 

    <xsl:choose> 
    <xsl:when test="normalize-space($needles) = ''"> 
     <xsl:value-of select="$accumulator"/> 
    </xsl:when> 
    <xsl:when test="contains($haystack, concat(' ', $needle, ' ')"> 
     <xsl:call-template name="countHits"> 
     <xsl:with-param name="needles" 
      select="substring-after($needles, ',')"/> 
     <xsl:with-param name="haystack" 
      select="$haystack"/> 
     <xsl:with-param name="accumulator" 
      select="1 + $accumulator"/> 
     </xsl:call-template> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:call-template name="countHits"> 
     <xsl:with-param name="needles" 
      select="substring-after($needles, ',')"/> 
     <xsl:with-param name="haystack" 
      select="$haystack"/> 
     <xsl:with-param name="accumulator" 
      select="$accumulator"/> 
     </xsl:call-template> 
    </xsl:otherwise>   
    </xsl:choose> 
</xsl:template> 

我認爲你可以計算出如何準備$草垛以及如何調用模板的初始值。

如果你有在XML形式的任何控制,你可能會考慮將「vehicleType」和「顏色」把單獨的值在不同的元素,即:

<colors> 
    <color>red</color> 
    <color>yellow</color> 
    <color>blue</color> 
    <color>white</color> 
    <color>purple</color> 
    <color>gold</color> 
    <color>silver</color> 
</colors> 

然後測試變得像以下(具有如上定義的$ haystack):

<xsl:variable name="colorcount" 
    select="count(/items/colors/color 
      [contains($haystack, concat(' ', ., ' ')])"/> 

和類似的車輛類型。

+0

謝謝@ Sperberg-McQueen。我確實擁有對XML的控制權,並按照您的建議更改了「vehecleType」和「color」(見上文)。但是,我無法讓它工作。我是XSL新手,你會介意發佈整個xsl嗎?我非常感謝你的幫助! –

+0

其實,是的,我會介意的。沒有冒犯性,但我在這個和其他大多數答案中的目標是向提問者展示如何幫助自己並幫助他們瞭解他們可能想要了解的主題以解決他們的問題。我避免提交完整的解決方案,因爲它們在幫助提問者學習方面效果不佳。 –

+0

我明白。謝謝你的幫助。 –

相關問題