0

次の入力 XML があります。

<?xml version="1.0" encoding="UTF-8"?>
<persons>
    <person id="1" type="parent">
        <name>father</name>
        <person-reference type="child">3</person-reference>
        <person-reference type="child">4</person-reference>
    </person>
    <person id="2" type="parent">
        <name>mother</name>
        <person-reference type="child">3</person-reference>
        <person-reference type="child">4</person-reference>
    </person>
    <person id="3">
        <name>brother</name>
    </person>
    <person id="4">
        <name>sister</name>
    </person>
</persons>

次の XSLT 変換を使用します。

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/persons">
        <relations>
            <xsl:apply-templates select="person[@type = 'parent']"/>
        </relations>
    </xsl:template>

    <xsl:template match="person">
        <parent>
            <name><xsl:value-of select="name"/></name>
        </parent>

        <xsl:apply-templates select="person-reference[@type = 'child']"/>
    </xsl:template>

    <xsl:template match="person-reference">
        <child>
            <name><xsl:value-of select="//person[@id = current()]/name"/></name>
        </child>
    </xsl:template>
</xsl:stylesheet>

この XML (結果) を取得します。

<?xml version="1.0" encoding="UTF-8"?>
<relations xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <parent>
        <name>father</name>
    </parent>
    <child>
        <name>brother</name>
    </child>
    <child>
        <name>sister</name>
    </child>
    <parent>
        <name>mother</name>
    </parent>
    <child>
        <name>brother</name>
    </child>
    <child>
        <name>sister</name>
    </child>
</relations>

しかし、私が望むのはこの XML (期待される結果) です:

<?xml version="1.0" encoding="UTF-8"?>
<relations xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <parent>
        <name>father</name>
    </parent>
    <child>
        <name>brother</name>
    </child>
    <child>
        <name>sister</name>
    </child>
    <parent>
        <name>mother</name>
    </parent>
</relations>

またはこれ(オプションの期待される結果):

<?xml version="1.0" encoding="UTF-8"?>
<relations xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <parent>
        <name>father</name>
    </parent>
    <parent>
        <name>mother</name>
    </parent>
    <child>
        <name>brother</name>
    </child>
    <child>
        <name>sister</name>
    </child>
</relations>

私のように参照を使用して二重出力を防止する XSLT の方法はありますか?

4

1 に答える 1