-2

嗨,我有一个 xml 文档,其中包含一个家庭列表和一个家庭成员列表,以及后来的家庭到家庭成员的链接。我必须创建一个 XSL,它以 Society
1 格式显示 HTML 表格内容。家庭
1.1 FamilyA
1.1.1 Member1
1.1.2 Member4
1.2 FamilyB
1.2.1 Member2
1.2.2 Member3
1.2.3 Member5
1.2.4 Member6

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

         <?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>
4

1 回答 1

1

以下 XSLT 1.0 解决方案仅解决组织数据的问题。我并不关心 html 渲染,因为这是一个微不足道的练习,最好留给 OP。此解决方案使用 for-each 和变量声明将输入文档的关系形式展开为输出中所需的分层形式。如果您需要 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>
于 2012-07-05T00:49:51.357 回答