XSLT 1.0 将多个重复元素复制到新父元素

XSLT 1.0 copy multiple repeating elements to new parents

我希望将一些重复元素移动到新的父元素中。我遇到的困难是处理嵌套在我试图转换的其他元素中的元素。下面的示例是一个简化版本。实际 XML 是一个更大的文档,并且有更多重复的块埋在其他块中。 当前XML:

    <root>
    <subject>
        <id>1</id>
        <subjectDetail>
            <address>
                <status>current</status>
                <street>Town Street</street>
                <town>Townsville</town>
            </address>
            <address>
                <status>previous</status>
                <street>Street Lane</street>
                <town>Springtown</town>
            </address>
        </subjectDetail>
    </subject>
    <subject>
        <id>2</id>
        <subjectDetail>
            <address>
                <status>current</status>
                <street>Rose Street</street>
                <town>Gardensville</town>
            </address>
            <address>
                <status>previous</status>
                <street>Violet Lane</street>
                <town>Gardensville</town>
            </address>
        </subjectDetail>
    </subject>
</root>

想要XML:

<root>    
<subjects>
        <subject>
            <id>1</id>
            <subjectDetail>
                <addresses>
                    <address>
                        <status>current</status>
                        <street>Town Street</street>
                        <town>Townsville</town>
                    </address>
                    <address>
                        <status>previous</status>
                        <street>Street Lane</street>
                        <town>Springtown</town>
                    </address>
                </addresses>
            </subjectDetail>
        </subject>
        <subject>
            <id>2</id>
            <subjectDetail>
                <addresses>
                    <address>
                        <status>current</status>
                        <street>Rose Street</street>
                        <town>Gardensville</town>
                    </address>
                    <address>
                        <status>previous</status>
                        <street>Tulip Street</street>
                        <town>Gardensville</town>
                    </address>
                </addresses>
            </subjectDetail>
        </subject>
    </subjects>
</root>

我不知道该怎么做。我试过嵌套 for-each 语句,但它似乎按顺序处理它们,我得到了重复的内容。这是我尝试过的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="/root/subject">
    <xsl:for-each select="/root/subject">
        <xsl:for-each select="/root/subject/subjectDetail/address">
                <xsl:element name="adresses">
        <xsl:element name="adress">
        <xsl:apply-templates select="@*|node()"/>
                </xsl:element>
        </xsl:element>
            </xsl:for-each>
            <xsl:element name="subjects">
            <xsl:element name="subject">
                <xsl:apply-templates select="@*|node()"/>
            </xsl:element>
            </xsl:element>
    </xsl:for-each>
</xsl:template>

这样的东西对你有用吗:

XSLT 1.0

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

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

<xsl:template match="/root">
    <xsl:copy>
        <subjects>
            <xsl:apply-templates select="subject"/>
        </subjects>
    </xsl:copy>
</xsl:template>

<xsl:template match="subjectDetail">
    <xsl:copy>
        <addresses>
            <xsl:apply-templates select="address"/>
        </addresses>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>