Jump to content

Create a new xml structure and select on it


grantdb

Recommended Posts

What I would like to do it temporarily (as in just load into memory, not write to a file) create something I can work on. My ulimate goal is to get a list of unique values (this bit I am fine with doing)I have something like this<book> <authorID1>1234</authorID1> <authorID2>5678</authorID1> <authorID3>9998</authorID1></book><book> <authorID1>5678</authorID1> <authorID2>1234</authorID1> <authorID3>9998</authorID1></book>I want to have something like this<myauthors> <authorIDNEW>1234</authorIDNEW> <authorIDNEW>5678</authorIDNEW> <authorIDNEW>9998</authorIDNEW> <authorIDNEW>5678</authorIDNEW> <authorIDNEW>1234</authorIDNEW> <authorIDNEW>9998</authorIDNEW></myauthors>Where I can then apply a select or whatever as if it was 'real' xmlIs that possible?ThanksGrant

Link to comment
Share on other sites

If you use an XSLT 2.0 processor like Saxon 9 or AltovaXML Tools you can simply create a variable e.g.

<xsl:variable name="v1">  <myauthors>   <xsl:for-each select="//book/authorID">	 <authorIDNEW><xsl:value-of select="."/></authorIDNEW>  </myauthors></xsl:variable>

and then use that variable later on with e.g. for-each select="$v1/myauthors/authorIDNEW" or apply-templates select="$v1/myauthors/authorIDNEW".With an XSLT 1.0 processor you face the problem that such a variable creates a result tree fragment on which you can only do copy-of select="$v1", unless you use an extension function like exsl:node-set to first transform the result tree fragment into a node-set you can use for-each or apply-templates this.

Link to comment
Share on other sites

Thanks Martin, just what I needed. I went for the 1.0 node-set (msxsl) approach. If anyone else is interested, this is what I did. This is for xsl-fo, but you should get the ideaGrant <fo:block>Let us prove we can access all the info</fo:block> <xsl:for-each select="/forrendering/RESULTS/DOCUMENTS/DOCUMENT"> <fo:block> <xsl:value-of select="AUTHOR1ID"/> <xsl:value-of select="AUTHOR2ID"/> <xsl:value-of select="AUTHOR3ID"/> <xsl:value-of select="AUTHOR4ID"/> <xsl:value-of select="AUTHOR5ID"/> <xsl:value-of select="AUTHOR6ID"/> </fo:block> </xsl:for-each> <xsl:variable name="getFullListAuthors"> <myauthors> <xsl:for-each select="/forrendering/RESULTS/DOCUMENTS/DOCUMENT"> <authorrecord> <authorID> <xsl:value-of select="AUTHOR1ID"/> </authorID> </authorrecord> <authorrecord> <authorID> <xsl:value-of select="AUTHOR2ID"/> </authorID> </authorrecord> <authorrecord> <authorID> <xsl:value-of select="AUTHOR3ID"/> </authorID> </authorrecord> <authorrecord> <authorID> <xsl:value-of select="AUTHOR4ID"/> </authorID> </authorrecord> <authorrecord> <authorID> <xsl:value-of select="AUTHOR5ID"/> </authorID> </authorrecord> <authorrecord> <authorID> <xsl:value-of select="AUTHOR6ID"/> </authorID> </authorrecord> </xsl:for-each> </myauthors> </xsl:variable> <fo:block>Let us prove that the variable has been populated</fo:block> <fo:block><xsl:value-of select="$getFullListAuthors"/></fo:block> <fo:block>This will output all the values</fo:block> <xsl:for-each select="msxsl:node-set($getFullListAuthors)/myauthors/authorrecord"> <fo:block><xsl:value-of select="authorID"/>--</fo:block> </xsl:for-each> <fo:block>And now lets get a list without any repeats</fo:block> <xsl:variable name="unique-authors" select="msxsl:node-set($getFullListAuthors)/myauthors/authorrecord[not(authorID=following::authorID)]"/> <xsl:for-each select="$unique-authors"> <xsl:sort select="authorID"/> <fo:block> <xsl:value-of select="authorID"/> </fo:block> </xsl:for-each>

If you use an XSLT 2.0 processor like Saxon 9 or AltovaXML Tools you can simply create a variable e.g.
<xsl:variable name="v1">  <myauthors>   <xsl:for-each select="//book/authorID">	 <authorIDNEW><xsl:value-of select="."/></authorIDNEW>  </myauthors></xsl:variable>

and then use that variable later on with e.g. for-each select="$v1/myauthors/authorIDNEW" or apply-templates select="$v1/myauthors/authorIDNEW".With an XSLT 1.0 processor you face the problem that such a variable creates a result tree fragment on which you can only do copy-of select="$v1", unless you use an extension function like exsl:node-set to first transform the result tree fragment into a node-set you can use for-each or apply-templates this.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...