问题
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:-
<?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>
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>
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>
Expected output.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>
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.
回答1:
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>
回答2:
Try next stylesheet:
<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>
来源:https://stackoverflow.com/questions/61107422/xml-cross-references-in-xslt