Hi,
I have different XML files of a book,
e.g. fm, part, chapter, BM, index, etc.... In the file index.xml there is a tag given with name "link" with "start" attribute which contains unique id,
e.g. <link start="c51351-963">. Now I have to check whether the @start id is available in the rest xml files with the tag name <target id="[id of @start]". If it is available then I have to check whether the parent tag is "fn". If the parent tag is "fn" then I have to create a new attribute in the "link" tag, in index.xml, like this <link start="c51351-963" TBlabel="fn3">.
I working with the approach where I'm storing all the xml files into a variable $docs and comparing the link id with target id. If its a match then it check the parent of matched <target id="">. I'm not sure is this the right way to check the parent "$docs//target[@id]/parent::fn" and geting the @label contents "$docs//target[@id]/parent::fn/label".
Please suggest.
Thanks,
Anil Yadav
XML files:
index.xml
Code:
<index-list>
<index-item><index-entry>Abou</index-entry><link start="c51351-963">236</link></index-item>
</index-list>
rest-files of book(I'm putting the coding of the matched file)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<main>
<chapter>
<title>Integration</title>
<body/>
<back>
<fn-group>
<fn><target id="c51351-961"/><label>1</label><p/></fn>
<fn><target id="c51351-962"/><label>2</label><p/></fn>
<fn><target id="c51351-963"/><label>3</label><p/></fn>
<fn><target id="c51351-964"/><label>4</label><p/></fn>
<fn><target id="c51351-965"/><label>5</label><p/></fn>
<fn><target id="c51351-966"/><label>6</label><p/></fn>
</fn-group>
</back>
</chapter>
</main>
</book>
Output required
index.xml
Code:
<index-list>
<index-item><index-entry>Abou</index-entry><link start="c51351-963" TBlabel="fn.3">236</link></index-item>
</index-list>
XSLT
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:variable name="docs" select="collection('file:///E:/Download/Index Sample/XML Sample/?select=*.xml')"/>
<xsl:template match="index-item">
<index-item>
<xsl:copy-of select="@*"/>
<xsl:if test="index-entry">
<xsl:apply-templates select="index-entry"/>
</xsl:if>
<xsl:for-each select="link">
<xsl:choose>
<xsl:when test="(@start=$docs//target/@id) and ($docs//target[@id]/parent::fn)">
<xsl:variable name="TBlabel" select="$docs//target[@id]/parent::fn/label"/>
<link>
<xsl:copy-of select="@*"/>
<xsl:attribute name="TBlabel">fn.<xsl:value-of select="normalize-space($TBlabel)"/></xsl:attribute>
<xsl:value-of select="."/>
</link>
</xsl:when>
<xsl:otherwise>
<link>
<xsl:copy-of select="@*"/>
<xsl:value-of select="."/>
</link>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</index-item>
</xsl:template>
</xsl:stylesheet>