拆分节点并将它们分配给变量进行匹配 [英] split nodes and assign them to variables for matching

查看:25
本文介绍了拆分节点并将它们分配给变量进行匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于根据属性值使用 xslt 合并两个元素,但尝试以不同的方式将其放在可以更好理解的地方.

This question is similar to merge two elements using xslt based on attribute values but trying to put this in different way where it could understand better.

我有一个 xml 文件,其中包含两个名称相同的元素,但第二个元素是第一个元素的一部分.示例:

I have an xml file with two elements whose names are same, but second element is part of first element. Example:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- This is first element-->
<book>
    <mbean code="org.book.mybooks"  name="mycompany.props.jndi:name=mybookprops">   
        <attribute name="bookprops">
            abc.mybook.onebook=@Value@
            def.mybook.twobook=@Value@
            ghi.myebook.threebook=@Value@
        </attribute>
    </mbean>
    <!--This is second element and is part of first element-->
    <book>
        <mbean code="org.book.mybooks"  name="mycompany.props.jndi:name=mybookprops">   
            <attribute name="bookprops">
            abc.mybook.onebook=@New_Value@
            def.mybook.twobook=@New_Value@
            ghi.myebook.fourbook=@Value@

            </attribute>
        </mbean>
    </book><!--closing tag of second element-->
</book><!--closing tag of first element-->

目标是:

将两个元素合并为一个元素,如果两个元素具有相似的节点且节点的值不同,则将第一个属性节点的值替换为第二个属性节点.

Combine both elements to one element and if both elements are having similar nodes with value of the nodes are different replace the values of first attribute nodes with second attribute nodes.

我正在考虑的程序:

  1. 对第一个和第二个元素的节点进行排序.
  2. 拆分第一个元素的节点并将其分配给变量.示例:第一个元素 abc.mybook.onebook=@Value@ 的节点被拆分为两个并将它们分配给变量 varaible1=abc.mybook.onebook 和 varaible2=@Value@.
  3. 类似的方式拆分第二个元素的节点并将它们分配给变量.第二个元素 abc.mybook.onebook=@New_Value@ 的示例节点被拆分为两个并将它们分配给变量 varaible3=abc.mybook.onebook 和 varaible4=@New_Value@.
  4. 现在将第一个元素变量与第二个元素变量进行匹配.示例:如果变量 1 等于变量 3,则用变量 4 替换变量 2;else 复制第一个元素节点并复制第二个元素节点.

这可以在 shell 或 bash 中很容易地完成,但由于要求与 xslt 有关,我正在尝试弄清楚如何实现.

This can be done very easily in shell or bash but since the requirement is to do with xslt, i am trying to figure it out how can this be accomplished.

我期望的最终输出是:

<book>
    <mbean code="org.book.mybooks"  name="mycompany.props.jndi:name=mybookprops">   
        <attribute name="bookprops">
            abc.mybook.onebook=@New_Value@
            def.mybook.twobook=@New_Value@
            ghi.myebook.threebook=@Value@
            ghi.myebook.fourbook=@Value@
        </attribute>
    </mbean>
</book>

推荐答案

我会使用 analyze-string 来提取部分然后你可以分组:

I would use analyze-string to extract parts and then you can group:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="xs mf">

<xsl:function name="mf:props" as="element(value)*">
  <xsl:param name="input" as="xs:string"/>
  <xsl:analyze-string select="$input" regex="(\S+)=(\S+)">
    <xsl:matching-substring>
      <value key="{regex-group(1)}"><xsl:value-of select="regex-group(2)"/></value>
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:function>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="book[book]/mbean/attribute">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:variable name="props">
      <xsl:sequence select="mf:props(.), mf:props(../following-sibling::book/mbean/attribute)"/>
    </xsl:variable>
    <xsl:text>&#10;</xsl:text>
    <xsl:for-each-group select="$props/value" group-by="@key">
      <xsl:apply-templates select="current-group()[last()]" mode="value-to-prop"/>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<xsl:template match="value" mode="value-to-prop">
  <xsl:value-of select="concat('            ', @key, '=', ., '&#10;')"/>
</xsl:template>

<xsl:template match="book/book"/>

</xsl:stylesheet>

Saxon 9.5 将您给定的输入转换为

Saxon 9.5 transforms your given input into

<?xml version="1.0" encoding="UTF-8"?><!-- This is first element--><book>
    <mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops">
        <attribute name="bookprops">
            abc.mybook.onebook=@New_Value@
            def.mybook.twobook=@New_Value@
            ghi.myebook.threebook=@Value@
            ghi.myebook.fourbook=@Value@
</attribute>
    </mbean>
    <!--This is second element and is part of first element-->
    <!--closing tag of second element-->
</book><!--closing tag of first element-->

这篇关于拆分节点并将它们分配给变量进行匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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