【问题标题】:XSLT for copying content from same-name elements into sibling nodesXSLT 用于将内容从同名元素复制到同级节点
【发布时间】:2019-06-28 13:51:32
【问题描述】:

我有一个 XML 文件,我需要在其中复制每个 节点的值并将其插入到对应的 节点中(覆盖,此处不为空)。

XML 文件具有以下结构:

<Message>
    <Id>VARIABLE_1</Id>
    <Code>VAR</Code>
    <Source>TEXT 1</Source>
    <Source>TEXT 2</Source>
    <Source/>
    <Source>TEXT 3</Source>
    <Comment/>
    <Target>SOMETHING 1</Target>
    <Target>SOMETHING 2</Target>
    <Target/>
    <Target>SOMETHING 3</Target>
    <Comment/>
</Message>

我需要将它“转换”成这样:

<Message>
    <Id>VARIABLE_1</Id>
    <Code>VAR</Code>
    <Source>TEXT 1</Source>
    <Source>TEXT 2</Source>
    <Source/>
    <Source>TEXT 3</Source>
    <Comment/>
    <Target>TEXT 1</Target>
    <Target>TEXT 2</Target>
    <Target/>
    <Target>TEXT 3</Target>
    <Comment/>
</Message>

所以,本质上,每个节点都会继承对应的前面节点的值。

总是会有匹配数量的 节点,但是有些父节点可能只包含一个 节点,而有些可能包含最多 5 个(在这个示例案例有 3 个)。

我已经尝试过这个 XSLT:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="//Target">
        <xsl:copy>
            <xsl:value-of select="//Target/preceding-sibling::Source"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

但它只复制第一个 节点并将其插入到每个后续 节点中。我需要将它们按顺序复制 - 第一个节点被复制到第一个 节点中,第二个 节点被复制到第二个 节点中,等等。

【问题讨论】:

    标签: xslt xpath


    【解决方案1】:

    怎么样:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Target">
        <xsl:variable name="i" select="count(preceding-sibling::Target) + 1" />
        <xsl:copy>
            <xsl:value-of select="../Source[$i]"/>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    或者,如果您愿意:

    <xsl:template match="Target">
        <xsl:copy>
            <xsl:value-of select="../Source[count(current()/preceding-sibling::Target) + 1]"/>
        </xsl:copy>
    </xsl:template>
    

    【讨论】:

    • 谢谢!这很好用。有没有办法将节点的处理限制在那些具有 VAR 子元素的父 元素?
    • 您可以使用&lt;xsl:template match="Target[../Code='VAR']"&gt; 将处理限制为具有兄弟Code 元素的Target 元素,其值为"VAR"
    【解决方案2】:

    一个简单的方法如下:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <!-- identity transform -->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <!-- Solution -->
        <xsl:template match="Target">        
            <xsl:copy>
                <xsl:value-of select="../Source[substring-after(current(),' ') = substring-after(.,' ')]"/>
            </xsl:copy>        
        </xsl:template>
    
    </xsl:stylesheet>
    

    输出符合预期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      相关资源
      最近更新 更多