2011-08-16 2 views
2

XML 설정 :하는 방법을 다시 숫자 필터링 요소

<?xml version="1.0" encoding="utf-8"?> 
<book> 
    <chapter> 
     <section name="a">...</section> 
     <section name="b">...</section> 
     <section name="c">...</section> 
     <section name="d">...</section> 
     <section name="e">...</section> 
     ... 
    </chapter> 
    <appendix> 
     <section name="reference">...</section> 
    </appendix> 
</book> 

안녕하세요, 저는 chapterappendix 노드에서 출력 모든 sections로합니다. 부록 아래에있는 섹션이 정확하게 인쇄됩니다. 그러나 장 아래의 모든 섹션이을 인쇄 할 수있는 것은 아니며 일부 외부 조건 (섹션 이름이 Java 응용 프로그램에서 전달 된 허용 목록에있는 경우)에 따라 다릅니다.

  • D ...

    원하는 결과

    1. B .. :

      또한 chapter 아래 sections은 다음과 같은 섹션 이름 전에 올바른 시퀀스 번호를 출력한다 .

    (섹션 a, c, e가 필터링됨을 의미 함)

    제 질문은 //chapter/section에 대해 위의 원하는 출력을 어떻게 생성 할 수 있습니까? 어떤 힌트 또는 도움이 높은

    을 감사

    내 XSL :

    <?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ex="http://example.com/namespace" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
        <xsl:output method="xml" indent="yes"/> 
    
        <xsl:param name="validChapters" /> 
    
        <xsl:function name="ex:isValidChapter" as="xs:boolean"> 
         <xsl:param name="str-in" as="xs:string"/> 
         <xsl:choose> 
          <xsl:when test="contains($validChapters, $str-in)"> 
           <xsl:sequence select="true()" /> 
          </xsl:when> 
          <xsl:otherwise> 
           <xsl:sequence select="false()" /> 
          </xsl:otherwise> 
         </xsl:choose> 
        </xsl:function> 
    
        <xsl:template match="/"> 
         <xsl:apply-templates select="chapter" /> 
         <xsl:apply-templates select="appendix" /> 
        </xsl:template> 
    
        <xsl:template match="chapter"> 
         ... 
         <xsl:apply-templates select="section" /> 
        </xsl:template> 
    
        <xsl:template match="appendix"> 
         ... 
         <xsl:apply-templates select="section" /> 
        </xsl:template> 
    
        <xsl:template match="section"> 
         ... 
         <xsl:apply-templates /> 
        </xsl:template> 
    
        <xsl:template match="//chapter/section"> 
         <xsl:if test="ex:isValidChapter(@name)"> 
          <fo:block> 
           <xsl:number format="1."/> 
           <xsl:value-of select="@name" /> 
          </fo:block> 
          <xsl:apply-templates /> 
         </xsl:if> 
        </xsl:template> 
    
        ... 
    </xsl:stylesheet> 
    
  • +0

    샘플 XML 데이터 및 원하는 출력을 통해보다 명확하게 설명하십시오. –

    +0

    "허용 목록"은 어디에 저장되어 있습니까? –

    +0

    허용 목록은 java 응용 프로그램에서 전달되며 쉼표로 구분 된 문자열 형식이므로 contains 함수를 사용합니다. –

    답변

    0

    대답은 당신의 <xsl:number> 명령에 count= 속성을 제공하는 것입니다 :

     <xsl:number format="1." count="section[ex:isValidChapter(@name)]"/> 
    

    을 따라서 섹션 번호에, "유효한"섹션 만 계산되므로 번호 매기기가 정확합니다. 당신은 말한다 템플릿 수정해야하는 것도

    참고 : <chapter> 이후

    <xsl:template match="/"> 
        <xsl:apply-templates select="chapter" /> 
    

    는 루트 노드의 자식이 아니다. (문서 요소의 하위 항목 인 <book>입니다.)

    <xsl:template match="/book"> 
        <xsl:apply-templates select="chapter" /> 
    

    또한 함수에 부울 결과를 부울로 바꾸기위한 선택/when/otherwise가 필요하지 않습니다. 다음과 같이 짧게 할 수 있습니다.

    <xsl:function name="ex:isValidChapter" as="xs:boolean"> 
        <xsl:param name="str-in" as="xs:string"/> 
        <xsl:sequence select="contains($validChapters, $str-in)"/> 
    </xsl:function> 
    
    관련 문제