根据大小拆分 xml 元素
Split xml element based on size
给定以下 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>
如果地址长度大于 30,我想执行 XSL 转换来拆分和复制元素。如果地址长度大于 30,第一个元素将包含前 30 个字符,新(复制的)元素将包含下一个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>
如何实现。在此先感谢您的帮助。
这最多只能处理 90 个字符的地址。但是,您可以根据模式向 AddressDetails 模板添加更多 if 来扩展它。
<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) > 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) > 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>
给定以下 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>
如果地址长度大于 30,我想执行 XSL 转换来拆分和复制元素。如果地址长度大于 30,第一个元素将包含前 30 个字符,新(复制的)元素将包含下一个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>
如何实现。在此先感谢您的帮助。
这最多只能处理 90 个字符的地址。但是,您可以根据模式向 AddressDetails 模板添加更多 if 来扩展它。
<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) > 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) > 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>