XSLT 中的 XML 交叉引用 [英] XML cross-references in XSLT
问题描述
以下是我拥有的输入 XML 和 XSL 文件.我需要将 XML 转换为 output.xml,如下所述.我遇到了组件标签的ref"和id"属性问题.
Below are the input XML and XSL files I have. I need to transform XML into output.xml as mentioned below. I am facing issues with "ref" and "id" attributes of the component tag.
input.xml:-
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 文件:
my xslt file:
<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 文件:-
my output.xml using above xslt and XML files:-
<?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.
I am trying to generate the expected output.xml file for which I have to write XSLT but got stuck at ref and id attributes of the component tag. I want to know where I am going wrong. This is the first time I am writing my own XSLT.
推荐答案
交叉引用最好使用 key.试试:
Cross-references are best resolved by using a key. Try:
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>
这篇关于XSLT 中的 XML 交叉引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!