XML到XSLT的转换

时间:2012-07-03 18:23:19

标签: xml xslt

您好我有一个xml文档,其中包含一个系列列表和一个familyMembers列表以及稍后的Family to FamilyMember链接。我必须创建一个显示内容的HTML表的XSL 在格式中 社会
  1.家庭
    1.1家庭A       1.1.1会员1       1.1.2会员4     1.2家庭B
      1.2.1会员2       1.2.2会员3
      1.2.3会员5       1.2.4会员6

点击链接应该导航到页面的相应部分。请帮我解决这个问题

         <?xml version="1.0" encoding="utf-8"?>
    <society>
        <families>
            <family>
                <name>A</name>
                <numberofMembers>2</numberofMembers>
                <Description>abc</Description>
                <place></place>
            </family>
            <family>
                <name>B</name>
                <numberofMembers>4</numberofMembers>
                <Description>xyz</Description>
                <place></place>
            </family>
        </families>
        <familyMembers>
            <familyMember>
            <name>member1</name>
            <occupation>blah</occupation>
            <ID>1</ID>
            </familyMember>
            <familyMember>
                <name>ghij</name>
                <occupation>blah2</occupation>
                <ID>2</ID>
            </familyMember>
            <familyMember>
                <name>member2</name>
                <occupation>blah3</occupation>
                <ID>3</ID>
            </familyMember>
            <familyMember>
                <name>member3</name>
                <occupation>bgd</occupation>
                <ID>4</ID>
            </familyMember>
            <familyMember>
                <name>member4</name>
                <occupation>sdjhf</occupation>
                <ID>5</ID>
            </familyMember>
            <familyMember>
                <name>member5</name>
                <occupation>member6</occupation>
                <ID>6</ID>
            </familyMember>
        </familyMembers>
        <FamilyFamilyMembers>
            <FamilyFamilyMember>
                <source>
                    /families/A
                </source>
                <target>
                    /familyMember/member1
                </target>
            </FamilyFamilyMember>
            <FamilyFamilyMember>
                <source>
                    /families/A
                </source>
                <target>
                    /familyMember/member4
                </target>
            </FamilyFamilyMember>
            <FamilyFamilyMember>
                <source>
                    /families/B
                </source>
                <target>
                    /familyMember/member2
                </target>
            </FamilyFamilyMember>
            <FamilyFamilyMember>
                <source>
                    /families/B
                </source>
                <target>
                    /familyMember/member3
                </target>
            </FamilyFamilyMember>
            <FamilyFamilyMember>
                <source>
                    /families/B
                </source>
                <target>
                    /familyMember/member5
                </target>
            </FamilyFamilyMember>
            <FamilyFamilyMember>
                <source>
                    /families/A
                </source>
                <target>
                    /familyMember/member6
                </target>
            </FamilyFamilyMember>
        </FamilyFamilyMembers>
    </society>

1 个答案:

答案 0 :(得分:1)

以下XSLT 1.0解决方案仅解决组织数据的问题。我并不关心html渲染,因为这是一个微不足道的练习,最好留给OP。此解决方案使用for-each和variable声明将输入文档的关系形式展开为输出中所需的分层形式。如果您需要XSLT 2.0解决方案,请告诉我们,因为XSLT 20解决方案将更简单,更小。

这个XSLT 1.0样式表...

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

<xsl:template match="/">
  <Families>
    <xsl:apply-templates select="society/families/family"/>
  </Families>
</xsl:template>

<xsl:template match="family">
 <xsl:variable name="family-name" select="name" />
 <family family-name="{name}">
  <xsl:variable name="roster" select="../../FamilyFamilyMembers/FamilyFamilyMember
       [normalize-space( substring-after( source, '/families/')) = $family-name]
      " />
    <xsl:for-each select="../../familyMembers/familyMember">
     <xsl:variable name="person-name" select="name" />
     <xsl:apply-templates select= "self::node()[
      count( $roster[normalize-space( substring-after( target, '/familyMember/')) = $person-name]) >= 1
      ]" /> 
    </xsl:for-each>  
 </family>
</xsl:template>

<xsl:template match="familyMember">
  <family-member member-name="{name}" /> 
</xsl:template>

</xsl:stylesheet>

...当应用于样本输入时,将产生......

<?xml version="1.0" encoding="utf-8"?>
<Families>
  <family family-name="A">
    <family-member member-name="member1" />
    <family-member member-name="member4" />
  </family>
  <family family-name="B">
    <family-member member-name="member2" />
    <family-member member-name="member3" />
    <family-member member-name="member5" />
  </family>
</Families>