使用XSLT

时间:2017-07-12 09:44:42

标签: xml xslt

我需要使用XSLT从XML文件动态输出数据。我们有一些不同的数据结构,而不是必须明确地遍历每个部分,我想知道是否有一种方法可以在一个代码块中完成。这是我将收到的XML示例:

        <IntermediaryInfo>
            <PersonalInformation>
                <Title>str1234</Title>
                <FirstName>str1234</FirstName>
                <Surname>str1234</Surname>
                <DateofBirth>2012-12-13</DateofBirth>
                <WorkTelephoneNumber>str1234</WorkTelephoneNumber>
            </PersonalInformation>
            <CompanyInformation>
                <CompanyName>str1234</CompanyName>
                <CompanyFCANumber>str1234</CompanyFCANumber>
            </CompanyInformation>
            <CompanyAddress>
                <PostCode>str1234</PostCode>
                <HouseNumber>str1234</HouseNumber>
                <HouseName>str1234</HouseName>
                <AddressLine1>str1234</AddressLine1>
                <AddressLine2>str1234</AddressLine2>
            </CompanyAddress>
            <CompanyPermissionLevels>
                <AdvisingonConsumer>str1234</AdvisingonConsumer>
                <ArrangingConsumerBuytoLetmortgageContracts>str1234</ArrangingConsumerBuytoLetmortgageContracts>
            </CompanyPermissionLevels>
            <PrincipleFirm>
                <FirmName>str1234</FirmName>
                <FirmFCANumber>123</FirmFCANumber>
            </PrincipleFirm>
            <LendingPartner>str1234</LendingPartner>
        </IntermediaryInfo>

有没有办法循环遍历每个节点并使用XSLT输出节点名称和值?所以它看起来像这样:

    PersonalInformation
    -------------------
    Title: str1234
    FirstName: str1234
    Surname: str1234
    DateofBirth: 2012-12-13
    WorkTelephoneNumber: str1234

    CompanyInformation
    ------------------
    CompanyName: str1234
    CompanyFCANumber: str1234

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

以下是您可以用作起点的内容:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />
<xsl:strip-space elements="*"/>

<xsl:template match="*">
    <xsl:param name="indent"/>
    <xsl:value-of select="$indent" />
    <xsl:value-of select="name()" />
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates>
        <xsl:with-param name="indent" select="concat($indent, '&#9;')"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="*[text()]">
    <xsl:param name="indent"/>
    <xsl:value-of select="$indent" />
    <xsl:value-of select="name()" />
    <xsl:text>: </xsl:text>
    <xsl:value-of select="." />
    <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,结果将是:

IntermediaryInfo
    PersonalInformation
        Title: str1234
        FirstName: str1234
        Surname: str1234
        DateofBirth: 2012-12-13
        WorkTelephoneNumber: str1234
    CompanyInformation
        CompanyName: str1234
        CompanyFCANumber: str1234
    CompanyAddress
        PostCode: str1234
        HouseNumber: str1234
        HouseName: str1234
        AddressLine1: str1234
        AddressLine2: str1234
    CompanyPermissionLevels
        AdvisingonConsumer: str1234
        ArrangingConsumerBuytoLetmortgageContracts: str1234
    PrincipleFirm
        FirmName: str1234
        FirmFCANumber: 123
    LendingPartner: str1234