Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

xslt - split XML element into many

this might be impossible, but u guys might have an answer, im tryin to split this xml

<CTP>
    <name>ABSA bank</name>
    <BAs.BA>bank|sector|issuer</BAs.BA>
</CTP>

and transform it to this form :

<CTP>
    <name>ABSA bank</name>
    <BAs>
        <BA>bank</BA>
        <BA>sector</BA>
        <BA>issuer</BA>
    </BAs>
</CTP>

i could do this using this code

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" version="1.0" extension-element-prefixes="str">
  <xsl:template name="str:tokenize">
    <xsl:param name="string" select="''" />
    <xsl:param name="delimiters" select="'&#x9;&#xA;'" />
    <xsl:choose>
      <xsl:when test="not($string) or (string-length($string)=0)" />
      <xsl:when test="not($delimiters) or (string-length($delimiters)=0)">
        <xsl:call-template name="str:_tokenize-characters">
          <xsl:with-param name="string" select="$string" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="str:_tokenize-delimiters">
          <xsl:with-param name="string" select="$string" />
          <xsl:with-param name="delimiters" select="$delimiters" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="str:_tokenize-characters">
    <xsl:param name="string" />
    <xsl:if test="$string">
      <BA>
        <xsl:value-of select="substring($string, 1, 1)" />
      </BA>
      <xsl:call-template name="str:_tokenize-characters">
        <xsl:with-param name="string" select="substring($string, 2)" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  <xsl:template name="str:_tokenize-delimiters">
    <xsl:param name="string" />
    <xsl:param name="delimiters" />
    <xsl:variable name="delimiter" select="substring($delimiters, 1, 1)" />
    <xsl:choose>
      <xsl:when test="string-length($delimiter)=0">
        <BA>
          <xsl:value-of select="$string" />
        </BA>
      </xsl:when>
      <xsl:when test="contains($string, $delimiter)">
        <xsl:choose>
          <xsl:when test="not(starts-with($string, $delimiter))">
            <xsl:call-template name="str:_tokenize-delimiters">
              <xsl:with-param name="string" select="substring-before($string, $delimiter)" />
              <xsl:with-param name="delimiters" select="substring($delimiters, 2)" />
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <BA />
          </xsl:otherwise>
        </xsl:choose>
        <xsl:call-template name="str:_tokenize-delimiters">
          <xsl:with-param name="string" select="substring-after($string, $delimiter)" />
          <xsl:with-param name="delimiters" select="$delimiters" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="str:_tokenize-delimiters">
          <xsl:with-param name="string" select="$string" />
          <xsl:with-param name="delimiters" select="substring($delimiters, 2)" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="/">
    <xsl:variable name="v1">
      <xsl:value-of select="CTP/BAs.BA" />
    </xsl:variable>
    <BAs>
      <xsl:call-template name="str:tokenize">
        <xsl:with-param name="string" select="$v1" />
        <xsl:with-param name="delimiters" select="'#'" />
      </xsl:call-template>
    </BAs>
  </xsl:template>
</xsl:stylesheet>

but the main challenge is to make it dynamic, in other words i want the 'BAs' and 'BA' Nodes to be dynamically written in case i got other XML like:

<CTP>
    <name>ABSA bank</name>
    <FAs.FA>dep|sec|issue</BAs.BA>
</CTP>

i want it to look like this:

<CTP>
    <name>ABSA bank</name>
    <FAs>
        <FA>bank</BA>
        <FA>sector</BA>
        <FA>issuer</BA>
    </FAs>
</CTP>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try it this way:

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="*[contains(name(), '.')]" name="nest">
    <xsl:param name="name" select="name()"/>
    <xsl:param name="delimiter" select="'.'"/>
    <xsl:choose>
        <xsl:when test="contains($name, $delimiter)">
            <xsl:element name="{substring-before($name, $delimiter)}" >
                <!-- recursive call -->
                <xsl:call-template name="nest">
                    <xsl:with-param name="name" select="substring-after($name, $delimiter)"/>
                </xsl:call-template>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="name" select="$name"/>
                <xsl:with-param name="text" select="."/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="name"/>
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'|'"/>
    <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
    <xsl:if test="$token">
        <xsl:element name="{$name}" >
            <xsl:value-of select="$token"/>
        </xsl:element>
    </xsl:if>
    <xsl:if test="contains($text, $delimiter)">
        <!-- recursive call -->
        <xsl:call-template name="tokenize">
            <xsl:with-param name="name" select="$name"/>
            <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Applied to the following test input:

XML

<CTP>
    <name>ABSA bank</name>
    <BAs.BA>bank|sector|issuer</BAs.BA>
    <FAs.FA.F>dep|sec|issue</FAs.FA.F>
</CTP>

the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<CTP>
   <name>ABSA bank</name>
   <BAs>
      <BA>bank</BA>
      <BA>sector</BA>
      <BA>issuer</BA>
   </BAs>
   <FAs>
      <FA>
         <F>dep</F>
         <F>sec</F>
         <F>issue</F>
      </FA>
   </FAs>
</CTP>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...