根据大小拆分xml元素



给定以下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) &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>

相关内容

  • 没有找到相关文章

最新更新