根据大小分割xml元素

问题描述 投票:0回答:1

给出以下XML示例文件:

<root>
<UserList>
    <UserDetails>
        <Info1>
            <Info1_Id>AA</Info1_Id>
        </Info1>
        <Info2>
            <Info2_Id>BB</Info2_Id>
        </Info2>
    </UserDetails>
    <UserAddress>
        <State>Maharastra</State>
        <AddressDetails>
            <City>Mumbai</City>
            <Address>Andheri Kurla Road, Mumbai</Address>
        </AddressDetails>
    </UserAddress>
    <UserAddress>
        <State>Karnataka</State>
        <AddressDetails>
            <City>Bangalore</City>
            <Address>ITPL Bangalore</Address>
        </AddressDetails>
        <AddressDetails>
            <City>Bangalore</City>
            <Address>XYZ Services Ltd in Whitefield Main Road, Bangalore</Address>
        </AddressDetails>
    </UserAddress>
</UserList>

我想执行XSL转换,以拆分并复制该元素,如果地址长度大于30。如果地址长度大于30,则第一个元素将包含前30个字符,而new(重复的)元素将包含下一个30个字符,依此类推。

预期输出:

<root>
<UserList>
    <UserDetails>
        <Info1>
            <Info1_Id>AA</Info1_Id>
        </Info1>
        <Info2>
            <Info2_Id>BB</Info2_Id>
        </Info2>
    </UserDetails>
    <UserAddress>
        <State>Maharastra</State>
        <AddressDetails>
            <City>Mumbai</City>
            <Address>Andheri Kurla Road, Mumbai</Address>
        </AddressDetails>
    </UserAddress>
    <UserAddress>
        <State>Karnataka</State>
        <AddressDetails>
            <City>Bangalore</City>
            <Address>ITPL Bangalore</Address>
        </AddressDetails>
        <AddressDetails>
            <City>Bangalore</City>
            <Address>XYZ Services Ltd in Whitefield</Address>
        </AddressDetails>
        <AddressDetails>
            <City>Bangalore</City>
            <Address> Main Road, Bangalore</Address>
        </AddressDetails>
    </UserAddress>
</UserList>

如何实现。在此先感谢您的帮助。

xml xslt xslt-1.0
1个回答
0
投票
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt ="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="exslt"> <xsl:output method="xml" indent="yes"/> <xsl:template match="AddressDetails"> <xsl:copy> <xsl:apply-templates select="node() | @*"> <xsl:with-param name="address" select="substring(Address, 1, 30)"/> </xsl:apply-templates> </xsl:copy> <xsl:if test="string-length(Address) &gt; 30"> <xsl:copy> <xsl:apply-templates select="node() | @*"> <xsl:with-param name="address" select="substring(Address, 31, 30)"/> </xsl:apply-templates> </xsl:copy> </xsl:if> <xsl:if test="string-length(Address) &gt; 60"> <xsl:copy> <xsl:apply-templates select="node() | @*"> <xsl:with-param name="address" select="substring(Address, 61, 30)"/> </xsl:apply-templates> </xsl:copy> </xsl:if> <!-- etc. --> </xsl:template> <xsl:template match="Address"> <xsl:param name="address"/> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:value-of select="$address"/> </xsl:copy> </xsl:template> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.