I'm learning XSL right now and have a question regarding cross-references. My target XML file is structured like this:
<XML>
<Model>
<packedElement typ="class" id="1"/>
<packedElement typ="class" id="2"/>
<packedElement typ="class" id="3"/>
</Model>
<Elements>
<Element idref="1">
<Attributes comment="comment 1."/>
</Element>
<Element idref="2">
<Attributes comment="comment 2."/>
</Element>
<Element idref="3">
<Attributes comment="comment 3."/>
</Element>
</Elements>
</XML>
I want to connect id=idref. My goal is to list all packedElements and print their comment. Can u guys help me?
I tried to resolve it with the key-funktion, but I wasn't very succesful.
Edit:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:key name="CommentK" match="Element" use="@idref"/>
<xsl:template match="XML">
<XML>
<xsl:apply-templates/>
</XML>
</xsl:template>
<xsl:template name="Start" match="packedElement">
<xsl:variable name="TEST" select="@id"/>
<xsl:variable name="Comment">
<xsl:call-template name="FindComment">
<xsl:with-param name="test2" select="@id"/>
</xsl:call-template>
</xsl:variable>
<content comment="{$Comment}" id ="{@id}" test="{$TEST}"></content>
</xsl:template>
<xsl:template name="FindComment">
<xsl:param name="test2"/>
<xsl:for-each select="key('CommentK', '$test2')">
<xsl:value-of select="Attributes/@comment"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XSLT version is 2.0. (Btw can someone tell me the difference between the .XSLT and .XSL?)
Extensible Stylesheet Language: Transformations (XSLT) is a language for transforming the structure of XML documents. It is designed for use as part of Extensible Stylesheet Language (XSL). XML Path Language (XPath) is an XSL sub-language that is designed to be used with XSLT.
Incorporating <STYLE> Elements into an XSLT FileAn XSLT style sheet can emit HTML <STYLE> elements, including CSS specifications, directly into the HTML that results from the XSLT transformation. This option works best when the number of CSS rules is small and easily managed.
XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.
The Extensible Stylesheet Language (XSL) is a W3C standard for describing presentation rules that apply to XML documents. XSL includes both a transformation language, (XSLT), and a formatting language. These two languages function independently of each other.
Try
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="el-by-idref" match="Elements/Element" use="@idref"/>
<xsl:template match="XML">
<xsl:copy>
<xsl:apply-templates select="Model/packedElement"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Model/packedElement">
<content comment="{key('el-by-idref', @id)/Attributes/@comment}" id="{@id}" test="{@id}"/>
</xsl:template>
</xsl:stylesheet>
that way you get
<XML>
<content comment="comment 1." id="1" test="1"/>
<content comment="comment 2." id="2" test="2"/>
<content comment="comment 3." id="3" test="3"/>
</XML>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With