简体   繁体   English

XSLT:具有嵌套属性的XML到XML

[英]XSLT: XML to XML with nested attributes

I've got 我有

<Level>
    <Stage>
        <Room id="1">
            <Door color="Brown" weight="5" check="true"/>
        </Room>
        <Room id="2">
            <Door color="Green" weight="7" check="false"/>
        </Room>
    </Stage>
</Level>

But i need it to transform it to following format with the help of XSLT 但是我需要它借助XSLT将其转换为以下格式

<Result>
    <Listing>
        <Room check="false">
            <Folder id="1" weight="5" color="Brown"/>
        </Room>
        <Room check="true">
            <Folder id="2" weight="7" color="Green"/>
        </Room>
    </Listing>
</Result>

The question is that Room's id should be in nested element descriptor, while nested check should be on the level up. 问题在于,Room的ID应该位于嵌套元素描述符中,而嵌套检查应位于更高级别。 Is it possible to to do with XSLT transformation? 有可能与XSLT转换有关吗? How? 怎么样? Could you provide some example please? 你能提供一些例子吗?

As per @michael's comment, it seems that your sample isn't really representative (since Rooms can have multiple Doors), and you will also need to determine the logic on how to project an aggregate boolean representation of all door check states onto the parent Room . 根据@michael的评论,您的示例似乎并没有真正的代表性(因为Rooms可以有多个Doors),并且您还需要确定如何将所有Door check状态的集合布尔表示形式投影到父级上的逻辑Room

Here's an example of mapping with multiple Doors, with the logic that all doors must be check='true' in order for the Room/@check to be true: 这是一个映射多个门的示例,其逻辑是所有门都必须为check='true'才能使Room/@check为true:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/Level">
        <Result>
            <Listing>
                <xsl:apply-templates select="Stage/Room"></xsl:apply-templates>
            </Listing>
        </Result>    
    </xsl:template>

    <xsl:template match="Room">
        <Room check="{count(Door[@check='false']) = 0 and Door}">
            <xsl:apply-templates select="Door"></xsl:apply-templates>
        </Room>
    </xsl:template>

    <xsl:template match="Door">
        <Folder id="{../@id}" weight="{@weight}" color="{@color}"/>
    </xsl:template>
</xsl:stylesheet>

Input Xml 输入Xml

<Level>
    <Stage>
        <Room id="1">
            <Door color="Brown" weight="5" check="true"/>
            <Door color="Purple" weight="7" check="true"/>
        </Room>
        <Room id="2">
            <Door color="Green" weight="7" check="false"/>
            <Door color="Orange" weight="4" check="true"/>
        </Room>
        <Room id="3">
        </Room>
    </Stage>
</Level>

Output Xml 输出Xml

<?xml version="1.0" encoding="utf-8"?>
<Result>
  <Listing>
    <Room check="true">
      <Folder id="1" weight="5" color="Brown" />
      <Folder id="1" weight="7" color="Purple" />
    </Room>
    <Room check="false">
      <Folder id="2" weight="7" color="Green" />
      <Folder id="2" weight="4" color="Orange" />
    </Room>
    <Room check="false" />
  </Listing>
</Result>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM