I get my required xml elements in block.But at the same, i also put block for catching other xml elements.But it is not worked for me…
This is XML Document
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:pPr>
</w:pPr>
<w:r>
<w:t>Text1-</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="Heading1" />
</w:pPr>
<w:r>
<w:t>Text2-</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
</w:pPr>
<w:r>
<w:t>Text3-</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
</w:pPr>
<w:r>
<w:t>Text4-</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="Heading1" />
</w:pPr>
<w:r>
<w:t>Text5-</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
This is XSLT File
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
exclude-result-prefixes="w">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Document>
<xsl:variable name="headingName" select="(//w:body/w:p/w:pPr/w:pStyle[starts-with(@w:val, 'Heading')])[1]/@w:val"/>
<xsl:variable name="topLevelHeadings" select = "//w:body/w:p[w:pPr/w:pStyle/@w:val = $headingName]"/>
<xsl:choose>
<xsl:when test="$topLevelHeadings">
<Heading>
<xsl:apply-templates select="$topLevelHeadings">
</xsl:apply-templates>
</Heading>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="w:p">
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</Document>
</xsl:template>
<xsl:template match="w:p">
<Paragraph>
<xsl:apply-templates />
</Paragraph>
</xsl:template>
<xsl:template match="w:r/w:t">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
My Generated Output is:
<Document>
<Heading>
<Paragraph>Text2-</Paragraph>
<Paragraph>Text5-</Paragraph>
</Heading>
</Document>
But My Required output is:
<Document>
<Paragraph>Text1-</Paragraph>
<Heading>
<Paragraph>Text2-</Paragraph>
</Heading>
<Paragraph>Text3-</Paragraph>
<Paragraph>Text4-</Paragraph>
<Heading>
<Paragraph>Text5-</Paragraph>
</Heading>
</Document>
I think, I have some problem with Block. So, Please Guide me to get out of this issue…
The problem with your xsl:choose here is that it is only being used once, and it is checking only if there is at least one w:p element with a heading. So, you will only get one Heading element output. For you xsl:choose to work, you really need to use it within the w:p template
However, you don’t really need the xsl:choose here. You can probably get away with just have a specific template to match w:p elements which have a matching w:pPr element
In this template you can then output the Heading element. You would then have a separate template to match all other w:p elements to output the paragraph, and if you give it a name, you could call it from the previous template too to share code
Here is the full XSLT
When applied to your input XML, the following is output
Actually, the final template for
<xsl:template match="w:r/w:t">isn’t strictly needed, because the default behaviour for XSLT when it matches an element for which there is not a specific template is to output the text anyway.