어떤 것이 있는 유용한 구조에 대한 사용 XSLT 생산 XSLT?
문제
기존 XSLT 스타일 시트는 XML 을 생산하고 잘 포맷 XHTML.를 만들고 싶어 XSL-FO 버전의이 스타일을 생성하 PDF 을 통해 아파치 FOP.내가 무엇을 알고 싶은 이것입니다:
어떤 사용하기 편리한 xslt 패턴 내가 배워야 할 것을 다음과 같:
- 복사 중 일부 변경되지 않은 노드
- 를 복사하의 노드지만,추가 특성
내가 만들 수 있는 새로운 노드를 사용하여
<xsl:element>
그러나 어떤 것이 있는 유용한 것들이 필요합니다.참고 하는 동안 나는 수행하지 않은 복사 중 하나에서 XSLT 형식을 다른 나는 톤의 XML>XHTML 을 통해 XSLT 그래서 나는 익숙해진 가장 핵심의 언어입니다.
해결책
당신이 찾고있는 패턴은 "수정 된 ID 변환"입니다. 이 접근법의 기초는 아래 스타일 시트의 첫 번째 템플릿 규칙 인 Identity Transform 규칙입니다. 그 후 각 규칙은 복사 동작에 대한 예외를 나타냅니다.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- But strip out <foo> elements (including their content) -->
<xsl:template match="foo"/>
<!-- For <bar> elements, strip out start & end tags, but leave content -->
<xsl:template match="bar">
<xsl:apply-templates/>
</xsl:template>
<!-- For <bat> elements, insert an attribute and append a child -->
<xsl:template match="bat">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="id">123</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
위의 내게 나에게 가장 만족스러운 것은 마지막 템플릿 규칙에서 발견 된 논리의 복제입니다. 하나의 속성을 추가하기위한 많은 코드입니다. 그리고 우리가 이것들이 필요한지 상상해보십시오. 다음은 우리가 재정의하려는 내용에서 더 정확하게 정밀하게 할 수있는 또 다른 접근법입니다.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates mode="add-atts" select="."/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- By default, don't add any attributes -->
<xsl:template mode="add-atts" match="*"/>
<!-- For <bat> elements, insert an "id" attribute -->
<xsl:template mode="add-atts" match="bat">
<xsl:attribute name="id">123</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
마지막으로, 이것은 당신이 만들고 싶은 각 종류의 편집에 대해 다른 모드를 사용하여 훨씬 더 발전 할 수 있습니다.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- For <bat> elements, insert an "id" attribute -->
<xsl:template mode="add-atts" match="bat">
<xsl:attribute name="id">123</xsl:attribute>
</xsl:template>
<!-- Append <new-element/> to <bat> -->
<xsl:template mode="append" match="bat">
<new-element/>
</xsl:template>
<!-- Insert an element in <foo> content -->
<xsl:template mode="insert" match="foo">
<inserted/>
</xsl:template>
<!-- Add content before the <bar/> and <bat/> elements -->
<xsl:template mode="before" match="bar | bat">
<before-bat-and-bar/>
</xsl:template>
<!-- Add content only after <bat/> -->
<xsl:template mode="after" match="bat">
<after-bat/>
</xsl:template>
<!-- Here's the boilerplate code -->
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:apply-templates mode="before" select="."/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates mode="add-atts" select="."/>
<xsl:apply-templates mode="insert" select="."/>
<xsl:apply-templates/>
<xsl:apply-templates mode="append" select="."/>
</xsl:copy>
<xsl:apply-templates mode="after" select="."/>
</xsl:template>
<!-- By default, don't add anything -->
<xsl:template mode="add-atts" match="*"/>
<xsl:template mode="insert" match="*"/>
<xsl:template mode="append" match="*"/>
<xsl:template mode="before" match="@* | node()"/>
<xsl:template mode="after" match="@* | node()"/>
</xsl:stylesheet>
XSLT 2.0에서는 다중 모드 템플릿 규칙 덕분에 일부 보일러 플레이트를 약간 단순화 할 수 있습니다.
<!-- By default, don't add anything -->
<xsl:template mode="add-atts
insert
append
before
after" match="@* | node()"/>
나는 때때로이 모든 사용자 정의 모드를 동일한 스타일 시트에 사용하지만, 필요한대로 게으르게 추가하지 않는 경우가 많습니다.
다른 팁
가장 큰 장애물을 변형 XSLT 는 네임스페이스의 출력 접두사로의 실제 XSL 지침에 변형시킵니다.당신이 사용하는 경우"xsl:"모두에서 당신의 XSL 지침과 출력,XSLT 엔진 알 수 없습니다 사이의 차이 XSL 지침에 그것을 실행해야 하고 그것을 출력하는,그래서 당신의 XSLT 지 않을 것이 구문 분석합니다.즉,을 사용하지 않는 경우에는 네임스페이스 별명:
<xsl:namespace-alias stylesheet-prefix="x" result-prefix="xsl"/>
이 교육은 내부에 배치 <xsl:stylesheet />
,쓰기를 할 수 있습니다 당신의 결과로그래밍에 변환을 사용하여 대체 namespace prefix.나중에 출력 문서가 만들어지는 접두사 당신은 실제로 원하는 것이에 삽입할 수 있는 별칭의 장소입니다.그래서 예를 들어,여기에서의 템플릿을 생성하는 템플릿에서 당신의 문서 출력:
<xsl:template match="xsl:template[@match='title']>
<x:template match="title>
<x:apply-templates />
</x:template>
</xsl:template>
여기에는 좋은 기사: http://www.xml.com/pub/a/2001/04/04/trxml/
과거에는 내가 개발 XSL-FO 스타일 시트를 다음 사용 렌더링-X FO2HTML 스타일 시트 변환하 XSL-FO HTML.변환 <block>
로 <div>
, <inline>
로 <span>
, 니다,등등.
난 그들을 사용하기 전에,하지만 당신은 고려할 수 있습니다력 HTML2FO 스타일 시트.거나 적어도 그들을보고를 통해 빌리는 몇 가지 아이디어.
때문에 HTML 부족의 일부를 매김을 구성하는 FO 제공되지 않을 수 있습을 줄 당신은 모든 것의 당신에 필요한 XSL-FO 출력할 수 있지만 아마 처리하는 대부분의 변환에서 논리 HTML XSL-FO 요소 속성의 몸에서 문서입니다.