XML перекрестных ссылок в XSLT - PullRequest
1 голос
/ 08 апреля 2020

Ниже приведены входные файлы XML и XSL, которые у меня есть. Мне нужно преобразовать XML в вывод. xml, как указано ниже. У меня проблемы с атрибутами ref и id тега компонента.

input. xml: -

<?xml version="1.0" encoding="UTF-8"?>

<site id="w123" name="website 1" description="Lorem ipsum">
    <views>
        <view id="p123" name="page 1">
            <description>Lorem ipsum</description>
            <component ref="wg123"/>
            <component ref="wg1234"/>
        </view>
        <view id="p234" name="page 2">
            <description>Lorem ipsum</description>
            <component ref="wg234"/>
            <component ref="wg2345"/>
            <component ref="wg123"/>
        </view>
    </views>
    <components>
        <component id="wg123" type="TEXT">
            <text>Lorem ipsum</text>
        </component>
        <component id="wg1234" type="IMG"
                   src="image-1234.jpg"/>
        <component id="wg234" type="YOUTUBE"
                   url="youtube.com/1234"/>
        <component id="wg2345" type="BUTTON"
                   label="Click Me"/>
    </components>
</site>

мой xslt-файл:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="site">
        <website>
            <xsl:attribute name="id">
                <xsl:value-of select="@id"/>
            </xsl:attribute>
            <xsl:attribute name="name">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <description>
                <xsl:value-of select="@description"/>
            </description>
            <xsl:apply-templates select="views/view"/>
        </website>
    </xsl:template>
    <xsl:template match="views/view">
        <page>
            <xsl:attribute name="id">
                <xsl:value-of select="@id"/>
            </xsl:attribute>
            <xsl:attribute name="name">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <description>
                <xsl:value-of select="description"/>
            </description>
            <xsl:for-each select="component">
                <widget>
                    <xsl:attribute name="id">
                        <xsl:value-of select="@ref"/>
                    </xsl:attribute>
                    <xsl:attribute name="type">
                        <xsl:for-each select="/site/components/component">
                            <xsl:value-of select="/site/components/component[current()/@ref]"/>
                        </xsl:for-each>
                    </xsl:attribute>
                   <!-- <xsl:value-of select="/site/components/component[current()/@ref]"/>-->

                </widget>
            </xsl:for-each>

        </page>
    </xsl:template>
</xsl:stylesheet>

мой вывод. xml с использованием вышеуказанных файлов xslt и XML: -

<?xml version="1.0" encoding="UTF-8"?>
<website id="w123" name="website 1">
   <description>Lorem ipsum</description>
   <page id="p123" name="page 1">
      <description>Lorem ipsum</description>
      <widget id="wg123" type="">
            Lorem ipsum
        </widget>
      <widget id="wg1234" type="">
            Lorem ipsum
        </widget>
   </page>
   <page id="p234" name="page 2">
      <description>Lorem ipsum</description>
      <widget id="wg234" type="">
            Lorem ipsum
        </widget>
      <widget id="wg2345" type="">
            Lorem ipsum
        </widget>
      <widget id="wg123" type="">
            Lorem ipsum
        </widget>
   </page>
</website>

Ожидаемый вывод. xml:

<website id="w123" name="website 1">
    <description>Lorem ipsum</description>
    <page id="p123" name="page 1">
      <description>Lorem ipsum</description>
        <widget id="wg123" type="TEXT">
           <text>Lorem ipsum</text>
        </widget>
        <widget id="wg1234" type="img" src="image-1234.jpg"/>
    <page id="p234" name="page 2">
    <description>Lorem ipsum</description>
    <widget id="wg234" type="YOUTUBE" url="youtube.com/1234"/>
    <widget id="wg2345" type="BUTTON" label="Click Me"/>
    <widget id="wg123" type="TEXT">
        <text>Lorem ipsum</text>
    </widget>
    </page>
</website>

Я пытаюсь сгенерировать ожидаемый вывод. xml файл, для которого я должен написать XSLT, но застрял в атрибутах ref и id тега компонента. Я хочу знать, где я иду не так. Это первый раз, когда я пишу свой собственный XSLT.

Ответы [ 2 ]

1 голос
/ 08 апреля 2020

Перекрестные ссылки лучше всего разрешать с помощью клавиши . Попробуйте:

XSLT 1.0

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

<xsl:key name="component" match="component" use="@id" />

<xsl:template match="/site">
    <website>
        <xsl:copy-of select="@id | @name | description"/>
        <xsl:apply-templates select="views/view"/>
    </website>
</xsl:template>

<xsl:template match="view">
    <page>
        <xsl:copy-of select="@id | @name | description"/>
        <xsl:apply-templates select="component"/>
    </page>
</xsl:template>

<xsl:template match="component">
    <xsl:variable name="component" select="key('component', @ref)" />
    <widget>
        <xsl:copy-of select="$component/@id | $component/@type | $component/@url | $component/@label | $component/text"/>
    </widget>
</xsl:template>

</xsl:stylesheet>
0 голосов
/ 09 апреля 2020

Попробуйте следующую таблицу стилей:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="site">
        <website>
            <xsl:attribute name="id">
                <xsl:value-of select="@id"/>
            </xsl:attribute>
            <xsl:attribute name="name">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <description>
                <xsl:value-of select="@description"/>
            </description>
            <xsl:apply-templates select="views/view"/>
        </website>
    </xsl:template>
    <xsl:template match="views/view">
        <page>
            <xsl:attribute name="id">
                <xsl:value-of select="@id"/>
            </xsl:attribute>
            <xsl:attribute name="name">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <description>
                <xsl:value-of select="description"/>
            </description>
            <xsl:for-each select="component">
                <widget>
                    <xsl:attribute name="id">
                        <xsl:value-of select="@ref"/>
                    </xsl:attribute>
                    <xsl:attribute name="type"><!-- next expression corrected -->
                      <xsl:value-of select="/site/components/component[@id=current()/@ref]/@type"/>
                    </xsl:attribute>
                   <!-- <xsl:value-of select="/site/components/component[current()/@ref]"/>-->

                </widget>
            </xsl:for-each>

        </page>
    </xsl:template>
</xsl:stylesheet>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...