2012-03-29 3 views
2

내가 패턴을 찾아 일치하는 특정 패턴 주위에 "묶어야 할 필요가있는 xml 파일이 파일에 이제XSLT의 XSL :. 분석 문자열 및 정규식 표현

<xml> 
<foo> 
    <id>1</id> 
    <description>This is section 1, this is section 1 or 2, this is section 1 and 2</description> 
</foo> 
</xml> 

나는 찾을 수있다 패턴 "섹션 1", "섹션 1 또는 2"및 "섹션 1 및 2"를 입력하고 일치하는 단어 주위에 "표시를하십시오.

나는이 같은 인 XSLT를 작성했습니다 :

<xsl:template match="text()"> 

<xsl:analyze-string select="." regex="section\s\d+|section\s\d+\s+or\s+\d|section\s\d+\s+and\s+\d"> 
    <xsl:matching-substring> 
    <xsl:if test="matches(.,'section\s\d+')" > 
     <xsl:text>"</xsl:text> 
     <xsl:value-of select="."/>  
     <xsl:text>"</xsl:text>  
    </xsl:if> 
    <xsl:if test="matches(.,'section\s\d+\s+or\s+\d')" > 
     <xsl:text>"</xsl:text> 
     <xsl:value-of select="."/>  
     <xsl:text>"</xsl:text>  
    </xsl:if> 
    <xsl:if test="matches(.,'section\s\d+\s+and\s+\d')" > 
     <xsl:text>"</xsl:text> 
     <xsl:value-of select="."/>  
     <xsl:text>"</xsl:text>  
    </xsl:if> 
    </xsl:matching-substring> 
    <xsl:non-matching-substring> 
    <xsl:value-of select="current()"/> 
    </xsl:non-matching-substring> 
</xsl:analyze-string> 
</xsl:template> 

내가 여기에 직면하고 문제는 내가 욕망 결과

을 받고 있지 않다 그래서 패턴 "섹션 1"뿐만 아니라 다른 모든 패턴과 일치하는 것입니다

상기 변환 결과는 난이 출력을 원하는

<xml> 
    <foo> 
     <id>1</id> 
     <description>This is "section 1", this is "section 1" or 2, this is "section 1" and 2</description> 
    </foo> 
</xml> 

이다.

<xml> 
    <foo> 
    <id>1</id> 
    <description>This is "section 1", this is "section 1 or 2", this is "section 1 and 2"</description> 
    </foo> 
</xml> 

아이디어를 구현하는 방법 ... 원하는 결과를 얻으십시오.

감사합니다.

이 귀하의 의견에 작업 표시

답변

3

:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="*"> 
    <xsl:copy> 
    <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="text()"> 
    <xsl:analyze-string select="." regex="section\s+\d+(\s+(or|and)\s+\d+)?"> 
    <xsl:matching-substring> 
    <xsl:text>"</xsl:text> 
    <xsl:value-of select="."/>  
    <xsl:text>"</xsl:text>  
    </xsl:matching-substring> 
    <xsl:non-matching-substring> 
    <xsl:value-of select="current()"/> 
    </xsl:non-matching-substring> 
    </xsl:analyze-string> 
</xsl:template> 

</xsl:stylesheet> 

당신은 그냥 요소를 문자열을 생성하는 경우, xsl:anayze-string 당신이 필요 이상이며,이 같은 결과를 생산하고 있지만 :

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="*"> 
    <xsl:copy> 
    <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="text()"> 
    <xsl:value-of select="replace(.,'section\s+\d+(\s+(or|and)\s+\d+)?','&quot;$0&quot;')"/> 
</xsl:template> 

</xsl:stylesheet> 
+1

David 덕분에 완벽하게 작동했습니다. "섹션 1 또는 2"로 제한되어 있기 때문에 "섹션 1 또는 2 또는 3"을 선택해야한다는 등의 일반화가 가능하다는 것을 알고 싶었습니다. – atif

+1

예, 변경 하시겠습니까? a *와 어쩌면 (또는 |)으로 바꾸거나 (또는 ​​|), 1, 2 또는 3이 잘 작동해야합니다. –

-1

을 빠르지 만 세련되지 않은 대답은 테스트가 (\ 's \ d \'s \ d ')와 일치하거나 (\'s \ d + \ s + 또는 \ s + \ d ') 일치하지 않는 것입니다.