2013-10-10 2 views
-3

개체의 유형과 이름을 설명하는 XML 요소가 있습니다. 개체는 유형별로 Animated = [Dog, Person] 및 Inanimate = [Plant, Automobile] 범주로 그룹화됩니다. 샘플 XML과 원하는 HTML이 아래에 나와 있습니다.XSLT : 유형별로 XML 요소를 그룹화하고 HTML 테이블로 출력

캐치 올 XSL 템플릿을 사용하여 어떤 유형 (예 : Cat, Animate/Inanimate 그룹에 매핑 할 수 없습니다.

저는 Saxon 9.4를 사용하고 있습니다.

XML :

<items> 
<item> 
    <type>Dog</type> 
    <name>Fido</name> 
</item> 
<item> 
    <type>Person</type> 
    <name>Bob</name> 
</item> 
<item> 
    <type>Plant</type> 
    <name>Tomato</name> 
</item> 
<item> 
    <type>Automobile</type> 
    <name>Honda</name> 
</item> 
<item> 
    <type>Automobile</type> 
    <name>Ford</name> 
</item> 

HTML :

<table> 
<th>There are 2 Animated objects</th> 
<tr> 
    <td>Dog Fido</td> 
    <td>Person Bob</td> 
</tr> 
</table> 

<table> 
<th>There are 3 Inanimate objects</th> 
<tr> 
    <td>Plant Tomato</td> 
    <td>Automobile Honda</td> 
    <td>Automobile Ford</td> 
</tr> 
</table> 

* 코멘트에 응답 *에 다음을 추가

일부 개체 유형과 Animate/Inanimate 그룹 간의 매핑은 경험적으로 알려져 있지만 매핑이 불완전합니다. 따라서이 겉보기 예에서 Cat 유형이 발생하면 포괄 형 템플리트에 인쇄해야합니다.

내가 겪었던 가장 큰 문제는 하나를 인쇄하고 복수 <tr>을 인쇄하는 것이 었습니다. match="items[item/type='Dog'] | items[item/type='Person']"이 있고 재귀 적으로 <xsl:apply-template select="items"/> 인 템플릿에 <th>을 인쇄 해 보았습니다. 그런 다음 Cat과 같은 유형이없는 항목의 캐치 올을 모두 수행하는 방법을 알지 못했습니다.

는 또한 match="item[type='Dog' or type='Person']"와 템플릿을하려고했으나 <th>을 인쇄하기 위해 나는 <table><th>..</th>를 인쇄 할 <xsl:if test="position() = 1">을해야했다. 그러나 그룹의 마지막 항목을 처리 할 때까지 XSLT에 닫는 </table>이 없기 때문에이 방법이 작동하지 않습니다.

나는 이것이 내 딜레마를 분명히하기를 바랍니다. 당신은 매핑 매개 변수를 정의 할 수 있습니다 XSLT 2.0을 가정

감사합니다,

알렉

+0

특히 무엇을 시도하고 어떻게 실패합니까? – kjhughes

+0

좋은 지적, 노력하지 마세요. 말할 것도없이 애니메이션이나 애니메이션을 해석하지 않는 것을 전혀 나타내지 않고 ... 아니면 XSLT 엔진에 애니메이션이 내장 될 것으로 기대합니까? 그것이 그것을 해석 할 수있는 것처럼. –

+0

애니메이트 대 무생물. 이것이 문제입니다. "dog"를 "animate"에 매핑하고 "automobile"을 "무해한"것으로 매핑하는 xml 맵을 제공하려고합니까 아니면 일부 긴 IF/THEN CHOOSE/OTHERWISE 일종의 기능 논리를 오른쪽으로 만들려고합니까? 지금까지 뭐 했니? 이것은 교과서에서 직접입니까? – kstubs

답변

1

은 당신이 원하는 :

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 

    <xsl:param name="map"> 
     <map> 
     <key name="Dog">Animated</key> 
     <key name="Person">Animated</key> 
     <key name="Plant">Inanimate</key> 
     <key name="Automobile">Inanimate</key> 
     </map> 
    </xsl:param> 

    <xsl:strip-space elements="*"/> 
    <xsl:output method="html" indent="yes" version="5.0"/> 

    <xsl:key name="k1" match="map/key" use="@name"/> 

    <xsl:template match="items"> 
     <xsl:for-each-group select="item" group-by="(key('k1', type, $map), 'unmapped')[1]"> 
     <table> 
      <thead> 
      <tr> 
       <th>There are <xsl:value-of select="count(current-group()), 
       current-grouping-key()"/> 
       objects.</th> 
      </tr> 
      </thead> 
      <tbody> 
      <xsl:apply-templates select="current-group()"/> 
      </tbody> 
     </table> 
     </xsl:for-each-group> 

    </xsl:template> 

    <xsl:template match="item"> 
     <tr> 
     <xsl:value-of select="*"/> 
     </tr> 
    </xsl:template> 

</xsl:stylesheet> 

입력 샘플이

<items> 
<item> 
    <type>Dog</type> 
    <name>Fido</name> 
</item> 
<item> 
    <type>Person</type> 
    <name>Bob</name> 
</item> 
<item> 
    <type>Plant</type> 
    <name>Tomato</name> 
</item> 
<item> 
    <type>Automobile</type> 
    <name>Honda</name> 
</item> 
<item> 
    <type>Automobile</type> 
    <name>Ford</name> 
</item> 
<item> 
    <type>Cat</type> 
    <name>Garfield</name> 
</item> 
</items> 

이 변환 그 방법 ~

<table> 
    <thead> 
     <tr> 
     <th>There are 2 Animated 
      objects. 
     </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr>Dog Fido</tr> 
     <tr>Person Bob</tr> 
    </tbody> 
</table> 
<table> 
    <thead> 
     <tr> 
     <th>There are 3 Inanimate 
      objects. 
     </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr>Plant Tomato</tr> 
     <tr>Automobile Honda</tr> 
     <tr>Automobile Ford</tr> 
    </tbody> 
</table> 
<table> 
    <thead> 
     <tr> 
     <th>There are 1 unmapped 
      objects. 
     </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr>Cat Garfield</tr> 
    </tbody> 
</table> 
+0

간단하게 훌륭합니다.마틴, 직접 연락 드릴 수는 있지만 웹 사이트의 문의 양식은 나와 협력하지 않습니다. – alecswan

관련 문제