如何合并具有公共属性值的xml节点

时间:2018-09-18 07:24:48

标签: xml xslt xpath data-conversion

我想获取一些XML文档并合并具有相同属性值的某个级别的所有节点。例如,如果我有这种形式的文档,

<xml>
 <books id="X">
  <book name="Story"/>
  <book name="Tale"/>
 </books>
 <books id="X">
  <book name="Folklore"/>
  <book name="Magic"/>
 </books>
 <books id="Y">
  <book name="Harry Potter"/>
  <book name="LotR"/>
 </books>
</xml>

然后我希望能够将其转换为以下文档:

<xml>
 <books id="X">
  <book name="Story"/>
  <book name="Tale"/>
  <book name="Folklore"/>
  <book name="Magic"/>
 </books>
 <books id="Y">
  <book name="Harry Potter"/>
  <book name="LotR"/>
 </books>
</xml>

如果可能的话,我非常希望有多种解决方案。例如,一个都使用XSLT,另一个则使用其他语言。

2 个答案:

答案 0 :(得分:0)

<xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="books[1][@id='X']">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
        <xsl:apply-templates/>
            <xsl:apply-templates select="following-sibling::books[@id='X']/book"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="books[@id='X' and preceding-sibling::books[@id='X']]"/>
    <xsl:template match="book">
        <xsl:copy>
        <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
check it.

答案 1 :(得分:0)

您可以尝试以下操作:

 public ProductionLineConfigViewModel VM { get; set; }

    public event EventHandler CanExecuteChanged;

    public UpdateProductionLineConfigCommand(ProductionLineConfigViewModel vm)
    {
        VM = vm;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        VM.updateProductionLineConfig();

    }

https://xsltfiddle.liberty-development.net/gWmuiJR处查看转化

相关问题