如何使用元素属性将一些xml转换为固定长度的字符串以定义值长度 [英] How can I transform some xml into a fixed length string using element attributes to define the value lengths

查看:81
本文介绍了如何使用元素属性将一些xml转换为固定长度的字符串以定义值长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <Destination>acme.com</Destination>
    <Record>
       <FirstField length="10">AAAA</FirstField>
       <SecondField length="15">BBBB</SecondField>
       <SubRecord>
          <ThirdField length="20">CCCC</ThirdField>
          <FourthField length="8">DDDD</FourthField>
       </SubRecord>
    </Record>
</Root>

我有一个要求以这个xml示例为例,其中节点内的元素将是动态的&可以深达几层,并使用一些可以处理xml直到完成的xslt转换,创建一个固定长度的文本字符串,在其中将每个值填充到左侧.每个值的长度都是在等长属性值中定义的.因此,转换后的上述示例将是(在引号内,以便您可以看到完整长度的字符串:

Hi, I have a requirement to take this xml example where the elements inside the node will be dynamic & can be several layers deep, and create a fixed length string of text where each value is padded out to the left, using some xslt transformation that can process the xml until complete.   The length of each value is defined in the constant length attribute value   So the above example after transformation would be (inside the quotes so you can see the full length string:

"      AAAA           BBBB                CCCC    DDDD"

我已经尝试了几次尝试来创建所需的xslt来进行转换,但是运气不佳,因为我对xslt的了解还不够.

I've tried several attempts to create the required xslt to transform this, but haven't had much luck, as I don't know enough about xslt.

有人能够提供可能有用的东西吗?需要安装在xsl 1.0中.

Is someone able to provide something that might work.   Needs to be in xsl 1.0.

谢谢.

推荐答案

尝试一下:

<xsl:variable name="spaces" select="'                                   '"/>
   <xsl:template match="/">
       <xsl:text>"</xsl:text>
       <xsl:for-each select="//*[@length]">
           <xsl:variable name="spacelenght" select="@length - string-length(.)"/>
           <xsl:value-of select="concat(substring($spaces, 1, $spacelenght), .)"/>
       </xsl:for-each>
       <xsl:text>"</xsl:text>
   </xsl:template>
   

输出

"      AAAA           BBBB                CCCC    DDDD"

请参见 https://xsltfiddle.liberty-development.net/6q1S8AC

针对您的更新问题

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">

    <xsl:output method="text"/>

    <xsl:variable name="spaces" select="'                                   '"/>
    
    <xsl:template match="/">
        <xsl:text>"</xsl:text>
            <xsl:apply-templates/> 
        <xsl:text>"</xsl:text>
    </xsl:template>
    
    <xsl:template match="node()" priority="-1">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="*[@Occurs]">
    <xsl:variable name="data">
        <xsl:apply-templates/>
    </xsl:variable>
    <xsl:call-template name="copy">
        <xsl:with-param name="data" select="$data"/>
        <xsl:with-param name="seq" select="0"/>
        <xsl:with-param name="lastseq" select="number(@Occurs)"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="copy">
    <xsl:param name="data"/>
    <xsl:param name="seq"/>
    <xsl:param name="lastseq"/>
    <xsl:copy-of select="$data"></xsl:copy-of>
    <xsl:if test="$seq &lt; $lastseq">
        <xsl:call-template name="copy">
            <xsl:with-param name="data" select="$data"/>
            <xsl:with-param name="seq" select="$seq + 1"/>
            <xsl:with-param name="lastseq" select="$lastseq"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
    
    <xsl:template match="*[@length]">
        <xsl:variable name="spacelenght" select="@length - string-length(.)"/>
        <xsl:value-of select="concat(substring($spaces, 1, $spacelenght), .)"/>
    </xsl:template>
    
</xsl:stylesheet>

输出

"      AAAA           BBBB                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD"

请参见 https://xsltfiddle.liberty-development.net/6q1S8AC/2

这篇关于如何使用元素属性将一些xml转换为固定长度的字符串以定义值长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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