2012-06-08 2 views
2

이 코드를 사용하여 150 자마다 XML 문자열을 구분합니다.줄 바꿈 횟수를 계산해야합니다.

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

여기 실행할 수 있습니다 : 여기

0

http://www.xmlplayground.com/7QAvNp는 XPath를 한 줄입니다 :

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

이 값은 XML 문서의 모든 텍스트 노드를 연결 한 총 NL 문자 수로 계산됩니다.

설명 :

translate(string(/), $vNL, '')는 문서 노드 어떤 NL 문자가 빈 문자열로 대체되어있는 다른 문자열 (XML 문서의 모든 텍스트 노드의 연결)의 문자열 값 (에서 생산 삭제됨).

그런 다음 전체 문자열 길이에서 이것을 빼서 (삭제 된) NL 문자 수인을 정확하게 제공합니다. 이 변환은 다음과 같은 XML 문서에 적용

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

: 다음 원하는

<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