Reputation: 85
I am trying to build below logic with xslt 2.0/1.0:
*[@name=summary]
go to the parent and look for all descendant <info>
elements (except @name=summary
)info/@id
access the document uri for @id
. And print the <name>
element from the xml document.@name=summary
and @level=top
, do not process <info>
with @duplicate=yes
XML
<map>
<info id="a.xml" level="top" name="summary"/>
<map id="b.xml">
<info id="c.xml" name="summary">
<info id="1.xml"/>
<info id="2.xml"/>
</info>
</map>
<map id="d.xml">
<info id="e.xml" name="summary">
<info id="1.xml" duplicate="yes"/>
<info id="4.xml"/>
<info id="2.xml" duplicate="yes"/>
</info>
</map>
<map id="f.xml">
<info id="g.xml" name="summary">
<info id="2.xml" duplicate="yes"/>
<info id="1.xml" duplicate="yes"/>
<info id="5.xml"/>
</info>
</map>
<info id="h.xml" level="top" name="summary"/>
</map>
Output
<result>
<summary id="a.xml" level="top">
<info id="1.xml">one</info>
<info id="2.xml">two</info>
<info id="4.xml">four</info>
<info id="5.xml">five</info>
</summary>
<summary id="c.xml">
<info id="1.xml">one</info>
<info id="2.xml">two</info>
</summary>
<summary id="e.xml">
<info id="1.xml">one</info>
<info id="4.xml">four</info>
<info id="2.xml">two</info>
</summary>
<summary id="g.xml">
<info id="2.xml">two</info>
<info id="1.xml">one</info>
<info id="5.xml">five</info>
</summary>
<summary id="h" level="top">
<info id="1.xml">one</info>
<info id="2.xml">two</info>
<info id="4.xml">four</info>
<info id="5.xml">five</info>
</summary>
</result>
1.xml, 2.xml, 4.xml, 5.xml, a.xml, b.xml... having similar structure (see comment below)
<?xml version="1.0" encoding="UTF-8"?>
<input>
<name>one</name>
<!-- 2.xml <name>two</name>, 4.xml <name>four</name>, 5.xml <name>five</name>, a.xml <name>a</name> ...... -->
</input>
Here is my xslt which does not give correct output:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="this" select="/"/>
<xsl:template match="/">
<result>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="*[@name = 'summary']">
<summary>
<xsl:copy-of select="@id"/>
<xsl:copy-of select="@level"/>
<xsl:copy-of select="@name"></xsl:copy-of>
<xsl:call-template name="createsummary"/>
</summary>
</xsl:template>
<xsl:template name="createsummary">
<xsl:for-each select="../descendant::*">
<xsl:for-each select="doc(resolve-uri(@id, document-uri($this)))/*">
<xsl:if test="*[name() = 'name']">
<xsl:copy-of select="name"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Upvotes: 1
Views: 779
Reputation: 167706
I haven't tried to set up the directory structure but the right info
elements are found by
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="this" select="/"/>
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<result>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="*[@name = 'summary']">
<summary>
<xsl:copy-of select="@id"/>
<xsl:copy-of select="@level"/>
<xsl:copy-of select="@name"></xsl:copy-of>
<xsl:apply-templates select="." mode="create-summary"/>
</summary>
</xsl:template>
<xsl:template match="*[@name = 'summary' and @level = 'top']" mode="create-summary">
<xsl:apply-templates select="../descendant::info[not(@name = 'summary') and not(@duplicate = 'yes')] except ." mode="summary"/>
</xsl:template>
<xsl:template match="*[@name = 'summary' and not(@level = 'top')]" mode="create-summary">
<xsl:apply-templates select="../descendant::info[not(@name = 'summary')] except ." mode="summary"/>
</xsl:template>
<xsl:template match="info" mode="summary">
<info id="{@id}"></info>
</xsl:template>
</xsl:stylesheet>
meaning the result is
<?xml version="1.0" encoding="UTF-8"?>
<result>
<summary id="a.xml" level="top" name="summary">
<info id="1.xml"/>
<info id="2.xml"/>
<info id="4.xml"/>
<info id="5.xml"/>
</summary>
<summary id="c.xml" name="summary">
<info id="1.xml"/>
<info id="2.xml"/>
</summary>
<summary id="e.xml" name="summary">
<info id="1.xml"/>
<info id="4.xml"/>
<info id="2.xml"/>
</summary>
<summary id="g.xml" name="summary">
<info id="2.xml"/>
<info id="1.xml"/>
<info id="5.xml"/>
</summary>
<summary id="h.xml" level="top" name="summary">
<info id="1.xml"/>
<info id="2.xml"/>
<info id="4.xml"/>
<info id="5.xml"/>
</summary>
</result>
So with the additional files you could change the template
<xsl:template match="info" mode="summary">
<info id="{@id}"></info>
</xsl:template>
to
<xsl:template match="info" mode="summary">
<info id="{@id}">
<xsl:value-of select="doc(resolve-uri(@id, document-uri($this)))/*/name"/>
</info>
</xsl:template>
and you should get what you want.
Upvotes: 1