如何在使用xslt2.0进行转换时保留xml元素的顺序?
问题描述:
这是XML文档。如何在使用xslt2.0进行转换时保留xml元素的顺序?
<?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:pStyle w:val="Heading1"/>
</w:pPr>
<w:r>
<w:t>Tables</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>Table1</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>row1col1</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>row1col2</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>row2col1</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>row2col2</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
<w:p>
<w:r>
<w:t>Table2</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>row11col11</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>row11col12</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>row12col11</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>row12col12</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
</w:body>
</w:document>
,我想用我的XSLT文件,该XML文档转换为下文提到的格式。
<Document>
<Heading1>
<title>Tables</title>
<paragraph>Table1</paragraph>
<table>
<paragraph>row1col1</paragraph>
<paragraph>row1col2</paragraph>
<paragraph>row2col1</paragraph>
<paragraph>row2col2</paragraph>
</table>
<paragraph>Table2</paragraph>
<table>
<paragraph>row11col11</paragraph>
<paragraph>row11col12</paragraph>
<paragraph>row12col11</paragraph>
<paragraph>row12col12</paragraph>
</table>
</Heading1>
</Document>
这是我送给你的参考XSLT文件...
<?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:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="html" 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">
<xsl:apply-templates select="$topLevelHeadings">
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="//w:p[w:r[w:t]]">
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</Document>
</xsl:template>
<xsl:template match="w:p">
<Paragraph>
<xsl:apply-templates select="./w:r/w:t"/>
</Paragraph>
<xsl:apply-templates select="descendant::w:p">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="w:t">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="//w:body/w:p[w:pPr[w:pStyle[starts-with(@w:val,'Heading')]]]">
<Heading1>
<Title>
<xsl:apply-templates select="./w:r/w:t"/>
</Title>
<xsl:choose>
<xsl:when test="following-sibling::w:tbl//w:p[w:r[w:t]]">
<xsl:for-each select="following-sibling::w:tbl">
<table>
<xsl:apply-templates select="descendant::w:p ">
</xsl:apply-templates>
</table>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="descendant::w:p">
<!-- | following-sibling::w:tbl//w:p[w:r[w:t]]-->
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="following-sibling::w:p[w:r and not(w:pPr[w:pStyle])] | following-sibling::w:p[w:r and not(w:pPr[w:pStyle[starts-with(@w:val,'Heading')]])]">
</xsl:apply-templates>
<xsl:variable name="nextHead" select="concat('Heading', number(substring-after('Heading1', 'Heading'))+1)"/>
<!-- Get a list of child nodes (headings) for the current node -->
<xsl:variable name="nextLevelHeadings" select="following-sibling::w:p[w:pPr[w:pStyle[@w:val=$nextHead]]]"/>
<!-- Apply recursively for next level headings within the scope -->
<xsl:apply-templates select="$nextLevelHeadings">
</xsl:apply-templates>
<!-- Close heading tag -->
</Heading1>
</xsl:template>
</xsl:stylesheet>
但输出是:
<Document>
<Heading1>
<Title>Table Manipulation</Title>
<table>
<paragraph>row1col1</paragraph>
<paragraph>row1col2</paragraph>
<paragraph>row2col1</paragraph>
<paragraph>row2col2</paragraph>
</table>
<table>
<paragraph>row11col11</paragraph>
<paragraph>row11col12</paragraph>
<paragraph>row12col11</paragraph>
<paragraph>row12col12</paragraph>
</table>
<Paragraph>Table1</Paragraph>
<Paragraph>Table2</Paragraph>
</Heading1>
</Document>
所以,请指导我得到这个问题,它会像我上面说的输出要求那样工作。因为我想用这个xml文件来转换改变段落或表格的改变顺序。
感谢&问候,
p.saravanan
答
我已经完成了任务,并感谢大家在这您的合作...
我只是连着说是工作absolutley罚款修改XSLT文件...
<?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:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="html" 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">
<xsl:apply-templates select="$topLevelHeadings">
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="//w:p[w:r[w:t]]">
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</Document>
</xsl:template>
<xsl:template match="w:p">
<Paragraph>
<xsl:apply-templates select="./w:r/w:t"/>
</Paragraph>
<xsl:apply-templates select="descendant::w:p">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="w:t">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="//w:body/w:p[w:pPr[w:pStyle[starts-with(@w:val,'Heading')]]]">
<Heading1>
<Title>
<xsl:apply-templates select="./w:r/w:t"/>
</Title>
<xsl:for-each select="following-sibling::*">
<xsl:choose>
<xsl:when test="descendant::w:p">
<table>
<xsl:apply-templates select="descendant::w:p ">
</xsl:apply-templates>
</table>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="self::w:p[w:r and not(w:pPr[w:pStyle])] | self::w:p[w:r and not(w:pPr[w:pStyle[starts-with(@w:val,'Heading')]])]">
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!--<xsl:apply-templates select="following-sibling::w:p[w:r and not(w:pPr[w:pStyle])] | following-sibling::w:p[w:r and not(w:pPr[w:pStyle[starts-with(@w:val,'Heading')]])]">
</xsl:apply-templates>-->
<xsl:variable name="nextHead" select="concat('Heading', number(substring-after('Heading1', 'Heading'))+1)"/>
<!-- Get a list of child nodes (headings) for the current node -->
<xsl:variable name="nextLevelHeadings" select="following-sibling::w:p[w:pPr[w:pStyle[@w:val=$nextHead]]]"/>
<!-- Apply recursively for next level headings within the scope -->
<xsl:apply-templates select="$nextLevelHeadings">
</xsl:apply-templates>
<!-- Close heading tag -->
</Heading1>
</xsl:template>
</xsl:stylesheet>
答
通过适当缩进你的代码,它表明你错过了</w:p>
。
我缩进并在这里添加它。这可能会或可能不会解决您的问题。
<w:document xmlns:w="w">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Heading1"/>
</w:pPr>
<w:r>
<w:t>Tables</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>Table1</w:t>
</w:r>
<w:tbl>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>row1col</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>row1co2</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>row2col1</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>row2col2</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
<w:p>
<w:r>
<w:t>Table2</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>row11col11</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>row11co12</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>row12col11</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>row12col12</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
</w:p>
<w:body>
</w:document>
我已经尝试过您提供的XML上的样式表(在修复前缀'w'的名称空间之后),但获得的结果与您在帖子末尾显示的输出不同。您确定这是您使用您提供的XML和XSLT获得的内容吗? –
@G_H:是的,它产生了和我在文章末尾提到的相同的输出。 – Saravanan
@Saravanan我复制了你的.xml文件和你的.xslt文件并运行转换。我没有得到你声称拥有的输出。 – FailedDev