2014-06-24 5 views
0

Ниже XSLT преобразует первый символ каждого слова в верхний регистр. Но не подходит для специального случая (пример: для ввода-> «лев, король» - «The Lion, King»). Нужно также решение для особого случая.Преобразование случая верблюда для специального случая также в XSLT

<xsl:template name="Split"> 
    <xsl:param name="pText"/> 
    <xsl:if test="string-length($pText)"> 
     <xsl:variable name="vConvertedWord" select="concat(translate(substring(substring-before(concat($pText,$vSeparator),$vSeparator),1,1), $smallcase,$uppercase),substring(substring-before(concat($pText,$vSeparator),$vSeparator),2))"/>  
        <xsl:value-of select="$vConvertedWord"/> 

     <xsl:value-of select="$vSeparator"/> 
     <xsl:call-template name="Split"> 
      <xsl:with-param name="pText" select="substring-after($pText,$vSeparator)"/> 
     </xsl:call-template> 

</xsl:template> 
+0

Невозможно воспроизвести вашу проблему. Либо информации недостаточно, либо данные, которые вы предоставили, неверны. См. Http://xsltransform.net/6qVRKvH. Измените свой вопрос и включите достаточную информацию, которая позволит диагностировать проблему. – helderdarocha

+0

@helderdarocha Я бы предположил, что разделитель - это пробел? –

+0

@ michael.hor257k Возможно, это так. – helderdarocha

ответ

0

Я думаю, что вы спрашиваете, что вы можете иметь несколько разделителей, а не только пространство, но запятая тоже? А также, может быть, вы хотите, чтобы некоторые «маленькие» слова не были капитализированы?

Только для моего личного вызова решение, с которым я столкнулся, состоит в том, чтобы перебирать текст по одному персонажу за раз, создавая «слово», пока не достигнете «разделителя». Затем вы можете загладить слово, которое вы создали, если только это не будет «маленьким» словом. Затем вы продолжаете итерацию новым словом, которое нужно создать.

Тест XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:variable name="smallcase">abcdefghijklmnopqrstuvwxyz</xsl:variable> 
    <xsl:variable name="uppercase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable> 
    <xsl:variable name="vSeparator"> ,.</xsl:variable> 
    <xsl:variable name="vProp"> a an the and </xsl:variable> 

    <xsl:template match="text()"> 
     <xsl:call-template name="Split"> 
      <xsl:with-param name="pText" select="." /> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="Split"> 
     <xsl:param name="pText"/> 
     <xsl:param name="letterPos" select="1" /> 
     <xsl:param name="currentWord" select="''" /> 

     <xsl:variable name="nextChar" select="translate(substring($pText, $letterPos, 1), $uppercase ,$smallcase)" /> 
     <xsl:variable name="separatorFound" select="($nextChar = '' or contains($vSeparator, $nextChar))" /> 

     <xsl:if test="$separatorFound"> 
      <xsl:choose> 
       <xsl:when test="contains($vProp, concat(' ', $currentWord, ' ')) and $letterPos &gt; string-length($currentWord) + 1"> 
        <xsl:value-of select="$currentWord" /> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="translate(substring($currentWord, 1, 1), $smallcase ,$uppercase)" /> 
        <xsl:value-of select="substring($currentWord, 2, string-length($currentWord) - 1)" /> 
       </xsl:otherwise> 
      </xsl:choose> 
      <xsl:value-of select="$nextChar" /> 
     </xsl:if> 

     <xsl:variable name="newWord"> 
      <xsl:if test="not($separatorFound)"><xsl:value-of select="concat($currentWord, $nextChar)"/></xsl:if> 
     </xsl:variable> 

     <xsl:if test="$letterPos &lt;= string-length($pText)"> 
      <xsl:call-template name="Split"> 
       <xsl:with-param name="pText" select="$pText" /> 
       <xsl:with-param name="letterPos" select="$letterPos + 1" /> 
       <xsl:with-param name="currentWord" select="$newWord" /> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

При применении к этому XML

<text>The Lion king, the tiger queen and the hippo prince</text> 

Ниже выводится

The Lion King, the Tiger Queen and the Hippo Prince 

РЕДАКТИРОВАТЬ - Альтернативное решение

Если вы хотите извлечь выгоду из письма, которые происходят после того, как , или ., то попробуйте этот XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:variable name="smallcase">abcdefghijklmnopqrstuvwxyz</xsl:variable> 
    <xsl:variable name="uppercase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable> 
    <xsl:variable name="vSeparator">,.</xsl:variable> 
    <xsl:variable name="vProp"> a an the and </xsl:variable> 

    <xsl:template match="text()"> 
     <xsl:call-template name="Split"> 
      <xsl:with-param name="pText" select="." /> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="Split"> 
     <xsl:param name="pText"/> 
     <xsl:param name="letterPos" select="1" /> 
     <xsl:param name="currentWord" select="''" /> 
     <xsl:param name="startOfBlock" select="1" /> 

     <xsl:variable name="nextChar" select="translate(substring($pText, $letterPos, 1), $uppercase ,$smallcase)" /> 
     <xsl:variable name="separatorFound" select="($nextChar = '' or $nextChar = ' ' or contains($vSeparator, $nextChar))" /> 

     <xsl:if test="$separatorFound"> 
      <xsl:choose> 
       <xsl:when test="contains($vProp, concat(' ', $currentWord, ' ')) and $startOfBlock = 0"> 
        <xsl:value-of select="$currentWord" /> 
       </xsl:when> 
       <xsl:otherwise> 
       <xsl:value-of select="translate(substring($currentWord, 1, 1), $smallcase ,$uppercase)" /> 
        <xsl:value-of select="substring($currentWord, 2, string-length($currentWord) - 1)" /> 
       </xsl:otherwise> 
      </xsl:choose> 
      <xsl:value-of select="$nextChar" /> 
     </xsl:if> 

     <xsl:variable name="newWord"> 
      <xsl:if test="not($separatorFound)"><xsl:value-of select="concat($currentWord, $nextChar)"/></xsl:if> 
     </xsl:variable> 

     <xsl:variable name="newStartOfBlock"> 
      <xsl:choose> 
       <xsl:when test="contains($vSeparator, $nextChar)"><xsl:value-of select="1" /></xsl:when> 
       <xsl:when test="$nextChar = ' ' and $currentWord != ''"><xsl:value-of select="0" /></xsl:when> 
       <xsl:otherwise><xsl:value-of select="$startOfBlock" /></xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 

     <xsl:if test="$letterPos &lt;= string-length($pText)"> 
      <xsl:call-template name="Split"> 
       <xsl:with-param name="pText" select="$pText" /> 
       <xsl:with-param name="letterPos" select="$letterPos + 1" /> 
       <xsl:with-param name="currentWord" select="$newWord" /> 
       <xsl:with-param name="startOfBlock" select="$newStartOfBlock" /> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

Когда применяется к этому XML

<text>The Lion king, the tiger queen and the hippo prince.... And cats, bats and rats.</text> 

Ниже выводится:

The Lion King, The Tiger Queen and the Hippo Prince.... And Cats, Bats and Rats. 
+0

Для ниже входа, льва король, тигр Королева и, бегемот принц мне нужен выход, как показано ниже (то есть, мне нужна заглавная буква после запятой (,) или точка (.)) Lion King, The Tiger Queen и The, Hippo Prince –

+0

Можете ли вы изменить свой вопрос, чтобы показать этот конкретный пример, тогда я могу отредактировать свой ответ соответственно. Спасибо –

+0

Выше код помог решить проблему. Спасибо всем :) –

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

  • Нет связанных вопросов^_^