【问题标题】:XML cross-references in XSLTXSLT 中的 XML 交叉引用
【发布时间】:2020-04-08 18:20:00
【问题描述】:

以下是我拥有的输入 XML 和 XSL 文件。我需要将 XML 转换为 output.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>

我的 output.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>

我正在尝试生成预期的 output.xml 文件,我必须为其编写 XSLT,但遇到了组件标记的 ref 和 id 属性。我想知道我哪里出错了。这是我第一次编写自己的 XSLT。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    最好使用 key 解决交叉引用问题。试试:

    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>
    

    【讨论】:

    • 谢谢。迈克尔,它非常适合我。感谢您的帮助。
    【解决方案2】:

    尝试下一个样式表:

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

    【讨论】:

    • 如果可行,您可以通过单击我的答案左侧的▲来支持 mi 解决方案。
    • 嗨,皮埃尔,我需要对提供的 XSLT 进行一些更正,然后它才能正常工作。我感谢您的帮助。谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    相关资源
    最近更新 更多