如何使用 xlst 在元素内拆分 xml 数据? [英] How to split xml data inside an element using xlst?

查看:24
本文介绍了如何使用 xlst 在元素内拆分 xml 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 xml 数据:

I have xml data like this:

<Invoice >
  <cac:AllowanceCharge>
  <cbc:ChargeIndicator>false</cbc:ChargeIndicator>

  <cbc:AllowanceChargeReason>ISK:y!#x!#w!#q!#t!#</cbs:AllowanceChargeReason>

  <cbc:MultiplierFactorNumeric>0.1</cbc:MultiplierFactorNumeric>
    </Invoice>

我必须用 !# 字符分割这个 AllowanceChargeReason,并将结果放入一个数组中.并用这个数组循环,结果我想得到这个结果.我如何为以下结果编写 xslt 代码?

I must split this AllowanceChargeReason by the !# characters, and put the results in an array. and loop with this array as a result i want to get this result. How can i write xslt code for below result?

<table>
<xsl:for-each >
<tr>
.......
</tr>
</xsl>
</table>

结果:

</table>
    <table>
    <tr>
      <td>
       y
      </td>
     <td>
       x
     </td>
     <td>
       w
     </td>
     <td>
       q
     </td>
    </tr> 
    </table>

推荐答案

要在 XSLT 1.0 中执行此操作,您需要创建一个命名模板,该模板将递归调用自身.它将以要分割的字符串作为参数,以及分割它的分隔符

To do this in XSLT 1.0, you would need to create a named template, which would call itself recursively. It would take as parameters a string to split, and the delimiter on which to split it

 <xsl:template name="split">
     <xsl:param name="text" select="." />
     <xsl:param name="delimiter" select="'!#'" />

请注意,在这种情况下,参数中的选择"值是默认值,并且仅在未将显式参数传递给模板时才适用.

Note, in this case the 'select' values in the parameters are default values, and would only apply if no explicit parameter is passed to the template.

在模板中,如果文本包含分隔符,您将输入文本

In the template, you would text if the text contains the delimiter

<xsl:choose>
   <xsl:when test="contains($text, $delimiter)">

如果是这样,您将使用 substring-before 输出字符串的第一部分,然后使用 substring-after

If so, you would output the first part of the string, using substring-before and then recursively call the named template using substring-after

  <td><xsl:value-of select="substring-before($text, $delimiter)" /></td>
  <xsl:call-template name="split">
       <xsl:with-param name="text" select="substring-after($text, $delimiter)" />
       <xsl:with-param name="delimiter" select="$delimiter" />
  </xsl:call-template>

当文本不包含分隔符时,直接输出即可.

When the text doesn't contain the delimiter, just output it.

例如,给定以下 XML

For example, given the following XML

<Invoice>
  <AllowanceChargeReason>ISK:y!#x!#w!#q!#t!#</AllowanceChargeReason>
</Invoice>

以及下面的 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="AllowanceChargeReason">
        <table>
            <tr>
                <xsl:call-template name="split">
                    <xsl:with-param name="text" select="substring-after(., ':')" />
                </xsl:call-template>
            </tr>
        </table>
    </xsl:template>

    <xsl:template name="split">
        <xsl:param name="text" select="." />
        <xsl:param name="delimiter" select="'!#'" />

        <xsl:if test="$text != ''">
            <xsl:choose>
                <xsl:when test="contains($text, $delimiter)">
                    <td><xsl:value-of select="substring-before($text, $delimiter)" /></td>
                    <xsl:call-template name="split">
                        <xsl:with-param name="text" select="substring-after($text, $delimiter)" />
                        <xsl:with-param name="delimiter" select="$delimiter" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <td><xsl:value-of select="$text" /></td>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

然后输出如下

<table>
   <tr>
      <td>y</td>
      <td>x</td>
      <td>w</td>
      <td>q</td>
      <td>t</td>
   </tr>
</table>

请注意,在将其应用于实际 XML 示例时,您必须考虑命名空间.

Do note you will have to take into account namespaces when you apply this to your actual XML sample.

这篇关于如何使用 xlst 在元素内拆分 xml 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆