简体   繁体   English

根据大小拆分xml元素

[英]Split xml element based on size

Given the following XML example file:给定以下 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>

I want to perform an XSL transformation to split and duplicate the element if Address length is more then 30. If address length is more then 30, First element will contain first 30 characters and new(duplicated) element will contain next 30 characters, so on.如果地址长度大于 30,我想执行 XSL 转换以拆分和复制元素。如果地址长度大于 30,第一个元素将包含前 30 个字符,新(重复)元素将包含接下来的 30 个字符,依此类推.

Expected output:预计 output:

<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>

How It can be achieved.如何实现。 Thanks in Advance for any help.在此先感谢您的帮助。

This will only handle addresses up to 90 characters.这只会处理最多 90 个字符的地址。 But, you can extend that by adding more if's to the AddressDetails template according to the pattern.但是,您可以根据模式向 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM