xml - XSL for-each on space-separated attribute values -
for research project, have document in xml/tei
<tei xmlns="http://www.tei-c.org/ns/1.0" > <body> <div> <p> <span target="#" type="passage" ana="tag957 tag874"> <span target="#" ana=""/> </span> <seg><date when="1980-01-01" type="date_seg"/>blabla blabla blabla blablablabla blablablabla blablablabla blablablabla bl </seg> <span target="#" type="passage" ana="tag1657 "> <span target="#" ana=""/> </span> <seg><date from="1980-01-03" to="1980-01-05" type="date_seg"/>blabla </seg> </p> </div> </body> </tei>
i need extract each tag contained in span/@ana : date , string length of following node seg. condition if incounter date attribute @from or @to, need value of @from. need :
tag957;1980-01-01;88 tag874;1980-01-01;88 tag1657;1980-01-03;11
i tried this, don't know how express for-each applyed 1 attribute's value @ time
<xsl:template match="tei:p"> <xsl:for-each select="tei:span"> <xsl:value-of select="./@ana"/> <xsl:text>;</xsl:text> <xsl:if test="following-sibling::tei:seg/tei:date/@from or following-sibling::tei:seg/tei:date/@to"> <xsl:value-of select="following-sibling::tei:seg/tei:date/@from"/> </xsl:if> <xsl:if test="following-sibling::tei:seg/tei:date/@when"> <xsl:value-of select="following-sibling::tei:seg/tei:date/@when"/> </xsl:if> <xsl:text>;</xsl:text> <xsl:value-of select="string-length(following-sibling::tei:seg)"/> <xsl:text>;</xsl:text> <xsl:value-of select="$newline"/> </xsl:for-each> </xsl:template>
using xslt 2.0 can use tokenize
identify different values in attribute value, can rewrite code as
<xsl:transform xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0" xpath-default-namespace="http://www.tei-c.org/ns/1.0"> <xsl:template match="p"> <xsl:for-each select="span"> <xsl:variable name="span" select="."/> <xsl:for-each select="tokenize(@ana, '\s+')[normalize-space()]"> <xsl:value-of select="."/> <xsl:text>;</xsl:text> <xsl:if test="$span/following-sibling::seg[1]/date/@from or $span/following-sibling::seg[1]/date/@to"> <xsl:value-of select="$span/following-sibling::seg[1]/date/@from"/> </xsl:if> <xsl:if test="$span/following-sibling::seg/date/@when"> <xsl:value-of select="$span/following-sibling::seg/date/@when"/> </xsl:if> <xsl:text>;</xsl:text> <xsl:value-of select="string-length($span/following-sibling::seg[1])"/> <xsl:text>;</xsl:text> <xsl:value-of select="' '"/> </xsl:for-each> </xsl:for-each> </xsl:template> </xsl:transform>
online @ http://xsltransform.net/jz1pupl.
Comments
Post a Comment