2011-09-21 3 views
1

누군가가 아래에서 고칠 수있는 가장 쉬운 방법을 말해 줄 수 있습니까? 나는 현재 상호 참조 (기본적으로 다른 페이지로의 링크)를 정의하는 다양한 방법을 포함하는 파일을 가지고 있으며, 그 중 2 개를 단일 형식으로 변환하려고합니다.XSLT 문자열 조작

<Paras> 
<Para tag="CorrectTag"> 
    <local xml:lang="en">Look at this section <XRef XRefType="(page xx)" XRefPage="36"/> for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
    <local xml:lang="en">Look at some other section <XRef XRefType="(page xx)" XRefPage="52"/> for more information</local> 
</Para> 
</Paras> 

에서 [외부 참조] 요소

<xsl:template match="XRef"> 
    <xsl:copy> 
     <xsl:attribute name="XRefType">(page xx)</xsl:attribute> 
     <xsl:choose> 
      <xsl:when test="@XRefType='(page xx)'"> 
       <xsl:attribute name="XRefPage" select="substring-before(substring-after(.,'(page '),')')"/> 
      </xsl:when> 
      <xsl:when test="@XRefType='xx'"> 
       <xsl:attribute name="XRefPage" select="."/> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:copy> 
</xsl:template> 

를 변환하는 XSLT 아래 사용 :

<Paras> 
<Para tag="CorrectTag"> 
<local xml:lang="en">Look at this section <XRef XRefType="(page xx)">(page 36)</XRef> for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
<local xml:lang="en">Look at some other section (page <XRef XRefType="xx">52</XRef>) for more information</local> 
</Para> 
</Paras> 

는 내가 달성하고자하는 것은 다음과 같다 : XML 아래의 간단한 예제를 보여주는 소스 형식입니다 이미 저에게이 출력을줍니다 :

<Paras> 
<Para tag="CorrectTag"> 
    <local xml:lang="en">Look at this section<XRef XRefType="(page xx)" XRefPage="36"/>for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
    <local xml:lang="en">Look at some other section (page<XRef XRefType="(page xx)" XRefPage="52"/>) for more information</local> 
</Para> 
</Paras> 

내 문제의 대부분을 이미 해결하고 있지만 나머지 다른 로컬 콘텐츠를 제거하지 않고도 나머지 [local] 요소를 정리하는 방법에 대해 고민하고 있습니다.

내가 필요한 것은 다음과 같습니다. "(페이지"다음에 XRef 요소가 오면 제거하십시오.) "X"문자열 앞에 "XRef"요소가 있으면 제거하십시오. 그렇지 않으면 터치하지 마십시오.

이 문제를 해결하는 방법에 대한 조언이 있으십니까?

감사합니다.

답변

1

템플릿으로 해결할 수 있어야합니다.

<xsl:template match="text()[ends-with(., '(page ')][following-sibling::node()[1][self::XRef]]"> 
    <xsl:value-of select="replace(., '(page $', '')"/> 
</xsl:template> 

<xsl:template match="text()[starts-with(., ')')][preceding-sibling::node[1][self::XRef]"> 
    <xsl:value-of select="substring(., 2)"/> 
</xsl:template> 

물론 텍스트 노드의 부모 요소에 대한 템플릿은 자식 노드를 처리하기 위해 apply-templates를 수행해야합니다.

+0

마틴에게 다시 한번 감사 드리며, 트릭을 수행합니다. 누군가가 재사용하기를 원한다면 작은 오타가 있습니다. [preceding-sibling :: node [1] [self :: XRef]]는 [preceding-sibling :: node() [self : XRef]] 일 필요가 있습니다. . – Wokoman