XSLT 1.0将逗号分隔的字符串拆分为命名节点

时间:2015-06-23 13:38:00

标签: regex xml xslt

客户提供的XML包含作为逗号分隔字符串的传递地址,我想使用XSLT 1.0将此字符串拆分为命名节点。

class WorkFacade
{
   public void Work(SimpleRequest simpleRequest)
   {
       ComplexRequest = new ComplexRequest(simpleRequest);
       ComplexRequest.RequestTime = DateTime.Now;
       ComplexRequest.UserId = 120;
       new ComplexWorker().Work(ComplexRequest, 2015, 6, 7, 23, 12, 1);
    }
}

class ComplexRequest
{
    ComplexRequest(SimpleRequest request)
    {
          // Code to convert simple request to complex request
          // understandable by ComplexRequest
    }
}

class ComplexWorker
{
   void Work(ComplexRequest request)
   {
      //...
   }
}

输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <text>
     Company, Streetaddress 20, 1234 AA, City
   </text>
</root>

我为XSLT 1.0尝试了几个递归模板,这些模板可以很好地分割,但生成的节点名称相同。

如果可能,如何使用XSLT 1.0实现这一目标?

3 个答案:

答案 0 :(得分:2)

它必须是递归模板吗?如此直观的substring-beforesubstring-after链如何:

  <xsl:template match="text">
    <xsl:copy>
      <COMPANY>
        <xsl:value-of select="normalize-space(substring-before(., ','))"/>
      </COMPANY>
      <xsl:variable name="s1" select="substring-after(., ',')"/>
      <ADDRESS>
        <xsl:value-of select="normalize-space(substring-before($s1, ','))"/>
      </ADDRESS>
      <xsl:variable name="s2" select="substring-after($s1, ',')"/>
      <ZIPCODE>
        <xsl:value-of select="normalize-space(substring-before($s2, ','))"/>
      </ZIPCODE>
      <CITY>
        <xsl:value-of select="normalize-space(substring-after($s2, ','))"/>
      </CITY>
    </xsl:copy>
  </xsl:template>

答案 1 :(得分:2)

为了它的乐趣,这是一个使用递归模板的通用版本。

<xsl:template match="text">
  <xsl:copy>
    <xsl:call-template name="parse-comma-separated">
      <xsl:with-param name="elements" select="'COMPANY,ADDRESS,ZIPCODE,CITY'"/>
      <xsl:with-param name="text" select="."/>
    </xsl:call-template>
  </xsl:copy>
</xsl:template>

<xsl:template name="parse-comma-separated">
  <xsl:param name="elements"/>
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="contains($elements, ',')">
      <xsl:element name="{normalize-space(substring-before($elements, ','))}">
        <xsl:value-of select="normalize-space(substring-before($text, ','))"/>
      </xsl:element>
      <xsl:call-template name="parse-comma-separated">
        <xsl:with-param name="elements" select="substring-after($elements, ',')"/>
        <xsl:with-param name="text" select="substring-after($text, ',')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:element name="{normalize-space($elements)}">
        <xsl:value-of select="normalize-space($text)"/>
      </xsl:element>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

答案 2 :(得分:0)

由于提供的XML只有地址作为单个节点(Streettaddress 20),我添加了第二个变量将地址字符串拆分为streetaddress和housenumber。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
  <xsl:template match="text">
   <root>
      <COMPANY>
        <xsl:value-of select="normalize-space(substring-before(., ','))"/>
      </COMPANY>
        <xsl:variable name="s1" select="substring-after(., ',')"/>
        <xsl:variable name="address_temp" select="normalize-space(substring-before($s1, ','))"/>
      <ADDRESS>
        <xsl:value-of select="normalize-space(substring-before($address_temp, ' '))"/>
      </ADDRESS>
      <HOUSENUMBER>
        <xsl:value-of select="normalize-space(substring-after($address_temp, ' '))"/>
      </HOUSENUMBER>
        <xsl:variable name="s2" select="substring-after($s1, ',')"/>
      <ZIPCODE>
        <xsl:value-of select="normalize-space(substring-before($s2, ','))"/>
      </ZIPCODE>
      <CITY>
        <xsl:value-of select="normalize-space(substring-after($s2, ','))"/>
      </CITY>
    </root>
  </xsl:template>
</xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8"?>
   <root>
   <COMPANY>Company</COMPANY>
   <ADDRESS>Streetaddress</ADDRESS>
   <HOUSENUMBER>20</HOUSENUMBER>
   <ZIPCODE>1234 AA</ZIPCODE>
   <CITY>City</CITY>
</root>