2013-02-11 2 views
0

XSLT를 사용하여 XML 문서를 변환하는 방법은 무엇입니까? 입력을 읽은 다음 문서의 모든 요소에 변형을 적용합니다 (예 : 선행 및 후행 공백을 잘라내어 반환). 그것의 완전한 구조를 가진 XML 문서? (또한 트림-문제에 대한 How can I trim space in XSLT without replacing repating whitespaces by single ones? 참조)모든 요소에 템플릿 적용 및 반환 XSLT

나는 모든 요소를 ​​복사 다음 코드로 시작 : 잘 작동

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

합니다.

<xsl:template match="@* | node()"> 
    <xsl:copy> 

     <xsl:apply-templates select="@* | node()"> 
     <xsl:call-template name="string-trim"> 
      <xsl:with-param name="string" select="@* | node()" /> 
     </xsl:call-template> 
     </xsl:apply-templates> 

    </xsl:copy> 
</xsl:template> 

을하지만 "적용 템플릿"태그 지정 안에 "통화 템플릿"태그 지정을 추가 할 수 없습니다 것으로 보인다 : 지금은 몇 줄을 추가하여 변환을 적용하고 싶었다.

변환을 각 요소에 적용하는 동안 원본 문서의 전체 구조를 대상 문서로 복사하는 방법은 무엇입니까?

답변

1

당신은

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="text()"> 
     <xsl:call-template name="string-trim"> 
      <xsl:with-param name="string" select="." /> 
     </xsl:call-template>     
    </xsl:template> 

    <xsl:template match="@*"> 
     <xsl:attribute name="{name()}"> 
      <xsl:call-template name="string-trim"> 
       <xsl:with-param name="string" select="." /> 
      </xsl:call-template>   
     </xsl:attribute> 
    </xsl:template> 

    <xsl:template name="string-trim"> 
     <xsl:param name="string"/> 
     ????? 
    </xsl:template> 

</xsl:stylesheet> 

는 "문자열 트림"당신과 함께라는 이름의 템플릿을 대체하는 것을 잊지 마세요 ... text()@*에 대해 별도의 템플릿을 가질 수 있습니다.

+0

도움 주셔서 감사합니다. –

관련 문제