简体   繁体   English

基于映射 - xslt拆分字符串

[英]split a string based on a mapping - xslt

As suggested. 如建议。 I am breaking the question here , into parts. 我在这里打破了这个问题。

My input xml, indicates the presence of fields in a string. 我的输入xml表示字符串中存在字段。 The input xml can have maximum 64 field elements. 输入xml最多可包含64个字段元素。 The input xml field elements always occur in ascending order. 输入xml字段元素始终按升序出现。 my input xml 我的输入xml

<Root>
  <element>field2</element>
  <element>field3</element>
  <element>field21</element>
</Root>

The string is defined as a variable in the xslt. 该字符串被定义为xslt中的变量。

my variable 我的变量

<xsl:variable name="inputstring" select="'013112316145ABC0812345678'"/>

The input xml says that field 2, 3 and 21 are the only fields in the string, to be extracted based on a mapping xml 输入xml表示字段2,3和21是字符串中唯一要根据映射xml提取的字段

Here is the mapping xml 这是映射xml

<Root>
  <field no="2" charlength="2">variable</field>
  <field no="3" total="4">fixed</field>
  <field no="21" charlength="2">
    <subfield no="1" total="3">fixed</subfield>
    <subfield no="2" charlength="2" idcode="ABC">variable</subfield>
  </field>
  <field no="63" charlength="2">
    <format1>
      <subfield no="1" total="3">fixed</subfield>
    </format1>
    <format2>
      <subfield no="1" total="3">fixed</subfield>
      <subfield no="2" total="7">fixed</subfield>
    </format2>
    <format3>
      <subfield no="1" total="3">fixed</subfield>
      <subfield no="2" total="7">fixed</subfield>
      <subfield no="3" total="6">fixed</subfield>
    </format3>
  </field>
</Root>

The mapping xml tells the following 映射xml告诉以下内容

  1. There are four types of fields, fixed, variable, field having subfields(with fixed and variable) and field having subfields(with different fomats) 有四种类型的字段,固定的,变量的,具有子字段的字段(具有固定和可变的)和具有子字段的字段(具有不同的fomats)
  2. Field number 2 is a variable field(as above), and the first two characters(charlength attribute) indicate the length of the field 字段编号2是可变字段(如上所述),前两个字符(charlength属性)表示字段的长度
  3. Field 3 is a fixed one, with a total of 4 characters. 字段3是固定的,总共4个字符。
  4. Field 21 is a field having subfields(with fixed and variable), where the first two chars(charlength) indicates the number of chars of the Field 字段21是具有子字段(具有固定和可变)的字段,其中前两个字符(charlength)指示字段的字符数
    • All fixed ones(subfields) occur first, followed by the variable subfields 首先发生所有固定的(子场),然后是可变子场
    • The subfields in this, always starts with the idcode(for 21's sub, it is ABC), followed by the length of characters(charlength attribute), then the subfield. 其中的子字段始终以idcode开头(对于21的sub,它是ABC),后跟字符长度(charlength属性),然后是子字段。 the length of chars can be 0 as well 字符长度也可以为0
    • All fixed and variable fields occur, the length of 0 indicates absence of a subfield(above point) 发生所有固定和可变字段,长度为0表示没有子字段(在点之上)
  5. Field 63 is a field having subfields(with different fomats), depending on the length of the field(charlength attribute), different formats are possible 字段63是具有子字段(具有不同的fomats)的字段,取决于字段的长度(charlength属性),可以使用不同的格式
    • For field 63, if the length is 03(first two chars, charlenghth attribute), it is format 1. If 10, format 2, if it is 16, then format3 对于字段63,如果长度为03(前两个字符,charlenghth属性),则为格式1.如果为10,则格式为2,如果为16,则为format3

My desired output xml 我想要的输出xml

<Root>
  <field2>3</field2>
  <!--value is 3 as the charlength is 2(which is 01)-->
  <field3>1123</field3>
  <!--field3 value is 1123 as it is fixed, total length of 4-->
  <field21>
    <subfield1>145</subfield1>
    <!--subfield1 should be 145 as it is fixed length of total 3 chars-->
    <subfield2>12345678</subfield2>
    <!--sufield2 starts with 'ABC', has length 08 chars-->
  </field21>
</Root>

Edit by Sean. 由肖恩编辑。

Break-down 分解

Here is a break-down of the mapping between input and output. 这是输入和输出之间映射的细分。

This is a picture of our string variable $inputstring 这是我们的字符串变量$ inputstring的图片

'013112316145ABC0812345678'

This is broken up into 3 fields according to the field definitions... 根据字段定义将其划分为3个字段...

013    -      1123  -  16145ABC0812345678
 |              |              v  
 v              v           field 21
field2        field3  

Let's break-down field 2: 让我们分解字段2:

 01    3
  |    v
  |   payload for field 2. This is output
  v
Contains the length(1) of the payload, which in this case is '01' = 1
This length of this 'header' is given by mapping Root/field[@no="2"]/@charlength
The "2" in this expression comes from the input document node at Root/element .

Lets break-down field 21: 让分解字段21:

16   145   ABC0812345678
 |    |       v
 |    |     subfield 2
 |    \ subfield 1
  \
   v
   Header for field 2. Says that the total field 2 length (header + subfield 1 +
subfield 2 consists of 16 characters. The length for this header was derived from
the mapping node at Root/field[@no="21"]/@charlength .

And for the final example: a break-down of field 21/ subfield 2. This is a picture of subfield 2 最后一个例子:字段21 /子字段2的细分。这是子字段2的图片

ABC   08   12345678
 |     |     |
 |     |     v
 |     |    This is the payload. It is output as the text node child of output
 |     |      subfield 2
 |     v
 v    Length of the following payload
 Signature. The length and value is equal to the mapping node
   Root/field[@no="21"]/subfield[@no="2"]/@idcode

Well..... I said I wouldn't do it, but I did it anyway. 嗯.....我说我不会这样做,但无论如何我做到了。

Caveats 注意事项

  1. Rule 5 (compound field with many formats) was not implemented. 规则5(具有多种格式的复合字段)未实施。 This was just too much work, so I leave the completion of Rule 5 to you. 这只是太多的工作,所以我将第5条的完成留给你。
  2. I tested on www.xmlper.com, so I used the msxsl:node-set(). 我在www.xmlper.com上测试过,所以我使用了msxsl:node-set()。 If not using MS, you may need to adjust slightly for your XSLT engine. 如果不使用MS,您可能需要稍微调整一下XSLT引擎。
  3. With this much string processing, you should really think hard about upgrading to XSLT 2.0 . 有了这么多的字符串处理,你应该认真考虑升级到XSLT 2.0。

Style-sheet 样式表

This XSLT 1.0 style-sheet... 这个XSLT 1.0样式表......

<xsl:stylesheet version="1.0"
   exclude-result-prefixes="xsl so msxsl"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:so="http://stackoverflow.com/questions/12035679"
   xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

    <xsl:variable name="inputstring" select="'013112316145ABC0812345678'" />

    <xsl:variable name="map">
        <so:mapping>
            <field no="2" charlength="2">variable</field>
            <field no="3" total="4">fixed</field>
            <field no="21" charlength="2">
                <subfield no="1" total="3">fixed</subfield>
                <subfield no="2" charlength="2" idcode="ABC">variable</subfield>
            </field>
        </so:mapping>
    </xsl:variable>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:call-template name="process-fields">
                <xsl:with-param name="element-stack" select="element" />
                <xsl:with-param name="code" select="$inputstring" />
            </xsl:call-template>
        </xsl:copy>
    </xsl:template>

    <xsl:template name="process-fields">
        <xsl:param name="element-stack" />
        <xsl:param name="code" />
        <xsl:if test="($code != '') and $element-stack">
            <xsl:variable name="field-no" select="
              substring-after($element-stack[1],'field')" />
            <xsl:variable name="field-parse-request">
                <so:field-parse-request code="{$code}">
                    <xsl:copy-of select="msxsl:node-set($map)/so:mapping/
                       field [@no=$field-no]" />
                </so:field-parse-request>
            </xsl:variable>
            <xsl:variable name="field-parse-result">
                <xsl:apply-templates
                    select="msxsl:node-set($field-parse-request)/*"
                    mode="field-parse" />
            </xsl:variable>
            <xsl:apply-templates
                   select="msxsl:node-set($field-parse-result)/so:output/*"
                   mode="remove-namespaces" />
            <xsl:call-template name="process-fields">
                <xsl:with-param name="element-stack"
                   select="$element-stack[position() &gt; 1]" />
                <xsl:with-param name="code"
                   select="msxsl:node-set($field-parse-result)/
                           so:output[1]/@code" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template match="so:field-parse-request[field[subfield]]"
                     mode="field-parse">
        <so:output>
            <xsl:variable name="header"
                  select="substring(@code,1,field/@charlength)" />
            <xsl:attribute name="code">
                <xsl:value-of
                   select="substring(@code,1+field/@charlength+$header)" />
            </xsl:attribute>
            <xsl:element name="field{field/@no}">
                <xsl:call-template name="process-subfields">
                    <xsl:with-param name="subfield-stack" select="field/subfield" />
                    <xsl:with-param
                      name="code"
                      select="substring(@code,1+field/@charlength,$header)" />
                </xsl:call-template>
            </xsl:element>
        </so:output>
    </xsl:template>

    <xsl:template match="so:field-parse-request[field[.='variable']]"
             mode="field-parse">
        <so:output>
            <xsl:variable name="header"
                   select="substring(@code,1,field/@charlength)" />
            <xsl:attribute name="code">
                <xsl:value-of select="substring(@code,1+field/@charlength+$header)" />
            </xsl:attribute>
            <xsl:element name="field{field/@no}">
                <xsl:value-of select="substring(@code,1+field/@charlength,$header)" />
            </xsl:element>
        </so:output>
    </xsl:template>

    <xsl:template match="so:field-parse-request[subfield[.='variable']]"
            mode="field-parse">
        <so:output>
            <xsl:variable name="header"
               select="substring( @code,
                                  1 + string-length( subfield/@idcode),
                                  subfield/@charlength)" />
            <xsl:attribute name="code">
                <xsl:value-of select="substring(
                     @code,
                     1 + string-length( subfield/@idcode) +
                         subfield/@charlength + $header)" />
            </xsl:attribute>
            <xsl:element name="subfield{subfield/@no}">
                <xsl:value-of select="
                   substring( @code, 
                     1 + string-length( subfield/@idcode) +
                       subfield/@charlength, $header)" />
            </xsl:element>
        </so:output>
    </xsl:template>

    <xsl:template match="so:field-parse-request[ field[.='fixed']] | so:field-parse-request[subfield[.='fixed']]" mode="field-parse">
        <so:output>
            <xsl:attribute name="code">
                <xsl:value-of select="substring(@code, (field/@total | subfield/@total) + 1)" />
            </xsl:attribute>
            <xsl:element name="{concat( name(field|subfield) ,field/@no | subfield/@no)}">
                <xsl:value-of select="substring(@code,1,field/@total | subfield/@total)" />
            </xsl:element>
        </so:output>
    </xsl:template>

    <xsl:template name="process-subfields">
        <xsl:param name="subfield-stack" />
        <xsl:param name="code" />
        <xsl:if test="($code != '') and $subfield-stack">
            <xsl:variable name="active-subfield-index">
                <xsl:choose>
                    <xsl:when test="not( $subfield-stack[1]/@idcode)">1</xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="
                           count($subfield-stack
                            [starts-with($code,@idcode)]/preceding-sibling::*)+1" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="field-parse-request">
                <so:field-parse-request code="{$code}">
                    <xsl:copy-of select="$subfield-stack[$active-subfield-index]" />
                </so:field-parse-request>
            </xsl:variable>
            <xsl:variable name="field-parse-result">
                <xsl:apply-templates
                  select="msxsl:node-set($field-parse-request)/*"
                  mode="field-parse" />
            </xsl:variable>
            <xsl:apply-templates
                select="msxsl:node-set($field-parse-result)/so:output/*"
                mode="remove-namespaces" />
            <xsl:call-template name="process-subfields">
                <xsl:with-param name="subfield-stack"
                  select="$subfield-stack[position() != $active-subfield-index]" />
                <xsl:with-param name="code"
                   select="msxsl:node-set($field-parse-result)/ 
                           so:output[1]/@code" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template match="*" mode="remove-namespaces">
        <xsl:element name="{local-name(.)}">
            <xsl:apply-templates select="@*|node()" mode="remove-namespaces" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*|text()" mode="remove-namespaces">
        <xsl:copy />
    </xsl:template>

</xsl:stylesheet>

Input 输入

...will take this input document... ......将采用此输入文件......

<Root>
  <element>field2</element>
  <element>field3</element>
  <element>field21</element>
</Root>

Output 产量

... and transform it according to all the stated rules, except rule 5, and produce this output... ...并根据规则5规定的所有规则对其进行转换,并生成此输出...

<Root>
  <field2>3</field2>
  <field3>1123</field3>
  <field21>
    <subfield1>145</subfield1>
    <subfield2>12345678</subfield2>
  </field21>
</Root>

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

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