2015-02-02 5 views
-1

나는 아래 XML을 가지고있다.조건이 될 필요가없는 곳으로 전화 받기

<?xml version="1.0" encoding="UTF-8"?> 
<body> 
    <para><content-style font-style="bold">18/8/7</content-style> <content-style font-style="italic">(2) Here is the content</content-style>&#x2014;Contributory negligence accident 
<content-style font-style="italic">second v/w datav. </content-style> </para> 
</body> 

및 아래의 XSL. 여기

 <?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:template match="/"> 
     <hmtl> 
     <head> 
      <title>New Version!</title> 
     </head> 
     <xsl:apply-templates/> 
     </hmtl> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="para[node()[position() = 1 and self::content-style[matches(., '(\w+)/(\w+)')]]]"> 
     <div class="para"> 
        <xsl:analyze-string select="." regex="(\w+)/(\w+)/(\w+)"> 
         <xsl:matching-substring> 
          <a name="{concat('P',regex-group(1),'-',regex-group(2),'-',regex-group(3))}"/> 
          <span class="phrase"> 
           <xsl:value-of select="."/> 
          </span> 
         </xsl:matching-substring> 
         <xsl:non-matching-substring> 
          <xsl:analyze-string select="." regex="(\w+)/(\w+)"> 
           <xsl:matching-substring> 
            <a name="{concat('P',regex-group(1),'-',regex-group(2))}"/> 
            <span class="phrase"> 
             <xsl:value-of select="."/> 
            </span> 
            <xsl:text>&#160;&#160;&#160;&#160;&#160;</xsl:text> 
           </xsl:matching-substring> 
           <xsl:non-matching-substring> 
            <xsl:value-of select="."/> 
           </xsl:non-matching-substring> 
          </xsl:analyze-string> 
         </xsl:non-matching-substring> 
        </xsl:analyze-string> 
      <xsl:apply-templates select="child::node()[not(self::content-style[1]/text())]"/> 
     </div> 
    </xsl:template> 

    <xsl:template match="content-style"> 
     <xsl:choose> 
      <xsl:when test="./@format"> 
       <span class="format-{@format}"> 
        <xsl:apply-templates/> 
       </span> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:variable name="fontStyle"> 
        <xsl:value-of select="concat('font-style-',@font-style)"/> 
       </xsl:variable> 
       <span class="{$fontStyle}"> 
        <xsl:apply-templates/> 
       </span> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:transform> 

내가 정규식이 모두 content-styles에서 호출지고있다 직면하고있는 문제,하지만 난 그것은 단지에 즉 단지에는 앞의 데이터 (텍스트 또는 노드)이없는 content-style에서 호출 할 노드는 <para><content-style>과 같지만 <para>(text/node)<content-style>은 아닙니다.

현재 O의/피 :

<div class="para"><a name="P18-8-7"></a><span class="phrase">18/8/7</span> (2) Here is the content—Contributory negligence accident 
      second <span class="font-style-italic">second v/w datav.</span> 
      </div> 

은 알려 주시기 바랍니다

<div class="para"><a name="P18-8-7"></a><span class="phrase">18/8/7</span> (2) Here is the content—Contributory negligence accident 
     second <a name="Pv-w"></a><span class="phrase">v/w</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; datav. —Contributory negligence accident 

     </div> 

예상 O/P는 어디에서 잘못되어 가고이고 나는이 문제를 어떻게 해결할 수 있습니다. Working Demo

감사

+1

나는이 모든 것의 논리를 이해하지 못한다고 생각합니다. 우선, 귀하의 실무 데모를 질문과 일치 시키십시오. 그것은 SO 질문과는 다른 입력과 템플릿을 가지고 있습니다. 그런 다음'para '와 일치하는 템플릿이 왜 두 개 있는지, 왜 이름이 템플릿인지, 왜'analyze-string'이 필요한지 설명하십시오. –

+0

안녕하세요 @ MathiasMüller, 응답 주셔서 감사합니다, 왜냐하면 내가 정규 표현식과 일치하는'content-style'에서 문구를 만들고 싶기 때문에이 정규 표현식을 사용합니다.이 구문은 목적을 연결하기위한 것일뿐입니다. 첫 번째 부분에서 ''을 선택하고 두 번째 부분은 ''과 일치하는 두 개의 일치가 있음을 나타내는 차별화를 보여줍니다.이 점을 지적 해 주셔서 감사합니다. 문제. – user3872094

+0

여전히 일관성이 없습니다. 'para'와 일치하는 첫 번째 템플릿을 제거했지만, 이제'@ align' 속성에 대한 부분은 사라졌습니다. 더 이상 중요하지 않습니까? '콘텐츠 스타일'이 첫 아이가 아닌 '파라'엘리먼트는 어떻게 될까요? 출력 샘플은 여전히'body' 또는'html'을 가지고 있지 않습니다. 원하든 원하지 않습니까? –

답변

1

당신이 (분석 - 문자열로) 당신의 정규식을 적용 할 때 순간에, 현재 XSLT를 보면 당신은 para 요소가 아닌 content-style에 위치, 그래서 select="."을 수행하면 얻을 것이다 첫 번째 노드뿐만 아니라 전체 para 요소의 문자열 값.

은 당신이 할 필요가 있습니다하면이로 변경, 그래서 그냥 처음 content-string

<xsl:analyze-string select="content-style[1]" regex="(\w+)/(\w+)/(\w+)"> 

도 있습니다 (당신이 알고있는 템플릿 일치의 조건에서 첫 번째 노드)을 분석 않습니다 현재 템플릿이 apply-templates에 문제가

<xsl:apply-templates select="child::node()[not(self::content-style[1]/text())]"/> 

나는 당신이 일을하려고하는 것은 떨어져 처음 child-element에서 para의 자식 노드를 선택 생각합니다. 그렇다면이 번호로 변경해야합니다.

<xsl:apply-templates select="child::node()[position() > 1]"/> 

이렇게하면 필요한 정보를 얻을 수 있습니다.

또는 대신 첫째 content-style 일치 하나에 para 일치 현재 템플릿을 변경할 수 있습니다 :이 거기에 조건이 있기 때문에

<xsl:template match="content-style[not(preceding-sibling::node())][matches(., '(\w+)/(\w+)')]"> 

, 그것은 일치하는 템플릿보다 더 높은 우선권을 가진다 content-style을 단독으로 사용하십시오. 아주 간단한 예를 들어

또한 적절한 회수는, 당신은 당신의 XSLT에 strip-space 요소를 추가해야 할 수도 있습니다, 기초 바와 같이, 제 1 content-style 전에, 그렇지 않으면 공백을

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="content-style[not(preceding-sibling::node())][matches(., '(\w+)/(\w+)')]"> 
     <content-style class="first"> 
      <!-- Regex here! --> 
      <xsl:apply-templates /> 
     </content-style> 
    </xsl:template> 

    <xsl:template match="content-style"> 
     <content-style class="other"> 
      <!-- Other --> 
      <xsl:apply-templates /> 
     </content-style> 
    </xsl:template> 
</xsl:stylesheet> 

참고이 XSLT 시도 마디.

<xsl:strip-space elements="*" />