简体   繁体   中英

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

<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. Is it possible to to do with XSLT transformation? 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 .

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:

<?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

<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 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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