How to split an attribute with values ​​separated by a character into separate elements

I have an attribute whose value can be one or more text strings separated by a comma. I want to convert using XSL the value of the attribute (s) into my own element;

eg

<post title='Hello World" tags="Test,Hello,World />

To which I would like to convert it:

<post>
<title>Hello World</title>
<tag>Test</tag>
<tag>Hello</tag>
<tag>World</tag>
</post>

Is it possible? TIA

+3
source share
3 answers

There are several ways to do this.

I. Using a recursively-named template in XSLT 1.0 This conversion :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="@*[not(name()='tags')]">
      <xsl:element name="{name()}">
        <xsl:value-of select="."/>
      </xsl:element>
    </xsl:template>

    <xsl:template match="@tags">
      <xsl:call-template name="tokenize">
        <xsl:with-param name="pText" 
         select="concat(., ',')"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template name="tokenize">
      <xsl:param name="pText"/>

      <xsl:if test="string-length($pText)">
        <tag>
          <xsl:value-of select=
           "substring-before($pText, ',')"/>
        </tag>

        <xsl:call-template name="tokenize">
          <xsl:with-param name="pText" select=
           "substring-after($pText, ',')"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:template>
</xsl:stylesheet>

when applied to the originally provided XML document (fixed to be well-formed):

<post title="Hello World" 
      tags="Test,Hello,World" />

creates the desired result :

<post>
   <title>Hello World</title>
   <tag>Test</tag>
   <tag>Hello</tag>
   <tag>World</tag>
</post>

II. / str-split-to-words FXSL 1.x

FXSL :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
>

   <xsl:import href="strSplit-to-Words.xsl"/>

   <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="@*[not(name()='tags')]">
      <xsl:element name="{name()}">
        <xsl:value-of select="."/>
      </xsl:element>
    </xsl:template>

    <xsl:template match="@tags">
    <xsl:variable name="vwordNodes">
      <xsl:call-template name="str-split-to-words">
        <xsl:with-param name="pStr" select="."/>
        <xsl:with-param name="pDelimiters" 
                  select="','"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
    </xsl:template>

  <xsl:template match="word">
    <tag>
      <xsl:value-of select="."/>
    </tag>
  </xsl:template>

</xsl:stylesheet>

XML-, , .

III. XPath 2.0 tokenize() XSLT 2.0

- XSLT 2.0.

XSLT 2.0:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="@*[not(name()='tags')]">
      <xsl:element name="{name()}">
        <xsl:value-of select="."/>
      </xsl:element>
    </xsl:template>

    <xsl:template match="@tags">
    <xsl:for-each select="tokenize(.,',')">
      <tag><xsl:value-of select="."/></tag>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

, XML-, .

+5

Dimitre, XSLT 1.0. "Value, Value, Value", .

, , CONCAT

tokenize xslt 1.

<xsl:template name="tokenize">
  <xsl:param name="pText"/>
  <xsl:param name="pTag"/>
  <xsl:if test="string-length($pText)">
    <xsl:element name="{$pTag}">
             <xsl:choose>
                  <xsl:when test="string-length(substring-before($pText, ','))">
                      <xsl:value-of select="substring-before($pText, ',')"/>
                  </xsl:when>
                  <xsl:otherwise><xsl:value-of select="$pText" /></xsl:otherwise>
              </xsl:choose>
    </xsl:element>

    <xsl:call-template name="tokenize">
      <xsl:with-param name="pText" select=
       "substring-after($pText, ',')"/>
      <xsl:with-param name="pTag"><xsl:value-of select="$pTag" /></xsl:with-param>
    </xsl:call-template>
  </xsl:if>

<xsl:call-template name="tokenize">
        <xsl:with-param name="pText">123,234,345</xsl:with>
        <xsl:with-param name="pTag">tag</xsl:with-param >
</xsl:call-template>

  <tag>123</tag>
  <tag>234</tag>
  <tag>345</tag>
+3

, , , , . , XML, , , . XML , , XML ?

0
source

Source: https://habr.com/ru/post/1703154/


All Articles