Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT将带点的平面xml转换为分层xml_Xslt - Fatal编程技术网

XSLT将带点的平面xml转换为分层xml

XSLT将带点的平面xml转换为分层xml,xslt,Xslt,我想使用XSLT转换下面的XML。这个XML元素有点来表示层次结构 <UsrEmployee> <Code>70068579</Code> <Initials>F</Initials> <FirstName>Koichi</FirstName> <Prefix></Prefix>

我想使用XSLT转换下面的XML。这个XML元素有点来表示层次结构

<UsrEmployee>
            <Code>70068579</Code>
            <Initials>F</Initials>
            <FirstName>Koichi</FirstName>
            <Prefix></Prefix>
            <LastName>Nakamura</LastName>
            <PropertyRef>70068579</PropertyRef>
            <SpaceRef.Code>001</SpaceRef.Code>
            <SpaceRef.FloorRef.Code>01</SpaceRef.FloorRef.Code>
            <SpaceRef.FloorRef.PropertyRef>70068579</SpaceRef.FloorRef.PropertyRef>
            <SpaceRef.propertyRef>70068579</SpaceRef.propertyRef>
        </UsrEmployee>
上面的XML我想转换为下面的XML,在源XML元素名称中可以是任何东西,点数(深度)是未知的(不固定的)。我想创建XSLT,它可以将任何大小的通用XML转换为层次结构

<UsrEmployee>
    <Code>70068579</Code>
    <Initials>F</Initials>
    <FirstName>Koichi</FirstName>
    <Prefix></Prefix>
    <LastName>Nakamura</LastName>
    <SpaceRef>
        <Code>001</Code>
        <propertyRef>70068579</propertyRef>
        <FloorRef>
            <Code>01</Code>
            <PropertyRef>70068579</PropertyRef>
        </FloorRef>
    </SpaceRef>     
    <PropertyRef>70068579</PropertyRef>
</UsrEmployee>

有人能帮我吗?

只需为“点”元素创建匹配模板并应用它们

使用
select
控制“一网打尽”标识模板(xslt中的最后一个模板)

当然,这可能是一个过于简单的解决方案,如果您有一个任意的层次结构,而这样选择模板是不可行的,则需要进行更复杂的转换

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

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="UsrEmployee">
        <xsl:element name="UsrEmployee">
            <xsl:apply-templates select="Code | Initials | FirstName | Prefix | LastName | PropertyRef" />

            <xsl:element name="SpaceRef">
                <xsl:apply-templates select="SpaceRef.Code | SpaceRef.propertyRef" />

                <xsl:element name="FloorRef">
                    <xsl:apply-templates select="SpaceRef.FloorRef.Code | SpaceRef.FloorRef.PropertyRef" />
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template match="SpaceRef.Code">
        <xsl:element name="Code">
            <xsl:value-of select="." />
        </xsl:element>
    </xsl:template>

    <xsl:template match="SpaceRef.propertyRef">
        <xsl:element name="propertyRef">
            <xsl:value-of select="." />
        </xsl:element>
    </xsl:template>

    <xsl:template match="SpaceRef.FloorRef.Code">
        <xsl:element name="Code">
            <xsl:value-of select="." />
        </xsl:element>
    </xsl:template>

    <xsl:template match="SpaceRef.FloorRef.PropertyRef">
        <xsl:element name="PropertyRef">
            <xsl:value-of select="." />
        </xsl:element>
    </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>


提供您的XSLT您是否提前知道所有可能的“点”元素?您知道哪些元素作为子元素可能有点元素吗?这不是一个真正的Java问题。对于任何语言或XSLT工具,问题和答案都是相同的post@JohnBollinger我将编辑我的问题。我无法控制点元素的深度和节点名称。它可以通过任何节点名进行命名。我正在寻找一些通用的。这应该适用于给定的输入。我之前没有具体说明。但我在找一些通用的东西。问题已编辑。