XSLT 根据关键字段值拆分为单独的段

问题描述 投票:0回答:1

我正在尝试编写 XSLT 映射来基于子段指令文本创建单独的指令段,只要指令文本段中存在名为 /NEW LINE/ 的关键字。我已经写了一个代码,但它给出了错误,请帮助我..

我附上了示例输入和输出,如下所示。请检查。

输入:

<?xml version="1.0" encoding="UTF-8"?>
<TIM>
    <SBDH>
        <FIELD1>132</FIELD1>
    </SBDH>
    <TI>
        
        <TIC>           
            <Instruction>
                <InstructionText languageCode="EN">20</InstructionText>
            </Instruction>
            <Instruction>
                <InstructionText languageCode="EN">234</InstructionText>
            </Instruction>
            <Instruction>
                <InstructionText languageCode="EN">456 /NEW LINE/ 789 /NEW LINE/ 910/NEW LINE/</InstructionText>
            </Instruction>
            
        </TIC>
    </TI>
</TIM>

** 所需输出:**

<?xml version="1.0" encoding="UTF-8"?>
<TIM>
    <SBDH>
        <FIELD1>132</FIELD1>
    </SBDH>
    <TI>
        <TIC>
            <Instruction>
                <InstructionText languageCode="EN">20</InstructionText>
            </Instruction>
            <Instruction>
                <InstructionText languageCode="EN">234</InstructionText>
            </Instruction>
            <Instruction>
                <InstructionText languageCode="EN">456</InstructionText>
            </Instruction>
            <Instruction>
                <InstructionText languageCode="EN">789</InstructionText>
            </Instruction>
            <Instruction>
                <InstructionText languageCode="EN">910</InstructionText>
            </Instruction>
        </TIC>
    </TI>
</TIM>

** 我使用的 XSLT 如下:**

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    <xsl:template match="/">
       
            <xsl:for-each select="//InstructionText">
            
                <xsl:for-each select="tokenize(string(InstructionText), '/NEW LINE/')">
                    <InstructionText>
                        <InstructionText><xsl:value-of select="."/></InstructionText>
                      
                     </InstructionText>
                </xsl:for-each>
            </xsl:for-each>
      
    </xsl:template>
</xsl:stylesheet>   

请在此提供帮助。

xslt xslt-1.0 xslt-2.0 xslt-3.0
1个回答
0
投票

如果您的处理器支持 XSLT 3.0,您应该能够执行以下操作:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="Instruction"> 
      <xsl:apply-templates/>
</xsl:template>

<xsl:template match="InstructionText">       
    <xsl:for-each select="tokenize(., '/NEW LINE/')[normalize-space(.)]">
        <Instruction>
            <InstructionText>
                <xsl:value-of select="normalize-space(.)"/>
            </InstructionText>
        </Instruction>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>   
© www.soinside.com 2019 - 2024. All rights reserved.