2012-06-08 2 views
2

Я использую этот код, чтобы сделать перерывы в XML-строке каждые 150 символов.Нужно подсчитать количество разрывов строк

<xsl:variable name="val_cr_ln" select=" '&#xA;' " /> 

Мне нужно сделать подсчет каждой разности этой переменной.

Может кто-нибудь сказать мне, как я это сделаю?

Благодаря

+3

"этот код" какой код? –

+0

+0

Вы хотите просто подсчитать количество раз, когда «val_cr_ln» встречается в документе XML? –

ответ

0

Чтобы найти число раз данная строка встречается в документе XML:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:exslstr="http://exslt.org/strings" 
> 

    <xsl:template match="/"> 
     <xsl:variable name='xml_as_str'> 
      <xsl:apply-templates select='*' /> 
     </xsl:variable> 
     <p>There are <xsl:value-of select='count(exslstr:split($xml_as_str, "string")) - 1' /> occurrences of "string" in there source XML.</p> 
    </xsl:template> 

</xsl:stylesheet> 

Может быть запущена здесь: http://www.xmlplayground.com/7QAvNp

0

Вот XPath один вкладыш :

string-length(/) - string-length(translate(string(/), $vNL, '')) 

Это оценивает общее количество символов NL в конкатенации всех текстовых узлов в документе XML.

Объяснение:

translate(string(/), $vNL, '') производит из строкового значения узла документа (конкатенации всех текстовых узлов в документе XML) другой строка, в которой любой NL характер был заменен на пустой строке (удален).

Затем, вычитая это из общей длины строки, дает нам точно количество (удаленных) символов NL.

Вот полный пример кода:

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

<xsl:variable name="vNL" select="'&#x0A;'"/> 

<xsl:template match="/"> 
    <xsl:value-of select= 
    "string-length(/) 
    - string-length(translate(string(/), $vNL, '')) 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

когда это преобразование применяется на следующий документ XML:

<text> 
Dec. 13 — As always for a presidential inaugural, security and surveillance were 
extremely tight in Washington, DC, last January. But as George W. Bush prepared to 
take the oath of office, security planners installed an extra layer of protection: a 
prototype software system to detect a biological attack. The U.S. Department of 
Defense, together with regional health and emergency-planning agencies, distributed 
a special patient-query sheet to military clinics, civilian hospitals and even aid 
stations along the parade route and at the inaugural balls. Software quickly 
analyzed complaints of seven key symptoms — from rashes to sore throats — for 
patterns that might indicate the early stages of a bio-attack. There was a brief 
scare: the system noticed a surge in flulike symptoms at military clinics. 
Thankfully, tests confirmed it was just that — the flu. 
</text> 

разыскиваемого, правильный результат получается:

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