This is my Xml Document.
<w:document xmlns:w="w">
<w:body>
<w:p>
<w:r>
<w:t>
Para1
</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>
Para2
</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>
Para3
</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>
Para4
</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>
Para5
</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>
Para6
</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>
Para7 <!-- Just Assume, this is current Node -->
</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
<w:p>开发者_JAVA技巧
<w:r>
<w:t>
Para8
</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
So, now i want to get the index position of current node that belonging to the child of <w:body>
.So My expected output is :6.
for example,
- if current node is para6 then my output is also 6.
- if current node is para8 then my output is 7.
- if current node is para5 then my output is 5.
If possible, Please Guide me to get this...
New Update:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
xmlns:exsl="http://exslt.org/common"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
extension-element-prefixes="exsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="*">
<Document>
<xsl:apply-templates select="//w:p">
</xsl:apply-templates>
</Document>
</xsl:template>
<xsl:template match="w:p">
<xsl:variable name="index">
<xsl:call-template name="get-para-index">
<xsl:with-param name="node" select="."/>
</xsl:call-template>
</xsl:variable>
<Paragraph>
<xsl:attribute name="index">
<xsl:value-of select="$index" />
</xsl:attribute>
<xsl:apply-templates select="./w:r/w:t"/>
</Paragraph>
</xsl:template>
<xsl:template match="w:t">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template name="get-para-index">
<xsl:param name="node"/>
<xsl:value-of select="count($node/ancestor::*[parent::w:body]/preceding-sibling::*)+1"/> <!-- Need to write logic here -->
</xsl:template>
</xsl:stylesheet>
I just calling the get-para-index template
for get the index position of each and every <w:p>
.But it returns always 1 for every <w:p>
. Please Guide me to get out of this problem...
Thanks & Regards, P.SARAVANAN
I'm not 100% sure of what you want but this expression
count(ancestor::*[parent::w:body]/preceding-sibling::*)+1
from the context node, finds the ancestor whose parent is <w:body>, then selects all preceding sibling elements and counts those. Adding 1 should give you the position.
My Updated Xslt that fulfilled my requests...
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
xmlns:exsl="http://exslt.org/common"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
extension-element-prefixes="exsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="*">
<Document>
<xsl:apply-templates select="//w:p">
</xsl:apply-templates>
</Document>
</xsl:template>
<xsl:template match="w:p">
<xsl:variable name="index">
<xsl:call-template name="get-para-index">
<xsl:with-param name="node" select="."/>
</xsl:call-template>
</xsl:variable>
<Paragraph>
<xsl:attribute name="index">
<xsl:value-of select="$index" />
</xsl:attribute>
<xsl:apply-templates select="./w:r/w:t"/>
</Paragraph>
</xsl:template>
<xsl:template match="w:t">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template name="get-para-index">
<xsl:param name="node"/>
<xsl:choose>
<xsl:when test="$node/ancestor::*[parent::w:body]">
<xsl:value-of select="count($node/ancestor::*[parent::w:body]/preceding-sibling::*)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="count($node/preceding-sibling::*)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
精彩评论