我嘗試根據 @style 對元素進行分組,所有情況看起來都很好,只是這里的問題是首先 p[@style="TRH7" 沒有在 TRH4 之前關閉。
我在這里參考但沒有運氣。
輸入 XML:
<?xml version="1.0" encoding="UTF-8"?>
<text width="12240" height="15840" orient="">
<p type="itemlist" id-rel="1" level="1" style="TRH2">
<r>Test scenario 1 (TRH2) /chapter/title</r>
</p>
<p type="itemlist" id-rel="1" level="6" style="TRH7">
<r>Test Scenario 2 (TRH7) /chapter/section/title</r>
</p>
<p style="TRNormal">
<r>Test Scenario 3 (TRNormal) /chapter/section/paragraph </r>
</p>
<table>a</table>
<p type="itemlist" id-rel="1" level="3" style="TRH4">
<r>Test Scenario 4 (TRH4) /chapter/division/title</r>
</p>
<p type="itemlist" id-rel="1" level="6" style="TRH7">
<r>Test Scenario 5 (TRH7) /chapter/division/section/title</r>
</p>
<p style="TRNormal">
<r>Test Scenario 6 (TRNormal) /chapter/division/section/paragraph </r>
</p>
<p type="itemlist" id-rel="1" level="4" style="TRH5">
<r>Test Scenario 7 (TRH5) /chapter/division/subdivision/title</r>
</p>
<p type="itemlist" id-rel="1" level="5" style="TRH6">
<r>Test Scenario 8 (TRH6) /chapter/division/subdivision/arabicSubdivision/title</r>
</p>
<p type="itemlist" id-rel="1" level="6" style="TRH7">
<r>Test Scenario 9 (TRH7) /chapter/division/subdivision/arabicSubdivision/section/title</r>
</p>
<p style="TRNormal">
<r>Test Scenario 10 (TRNormal)
/chapter/division/subdivision/arabicSubdivision/section/paragraph </r>
</p>
<p style="TRNormal">
<r>Test 1 </r>
</p>
<p style="TRNormal">
<r>Test 2 </r>
</p>
</text>
當前 XSL:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" method="xml"/>
<xsl:function name="mf:group" as="node()*">
<xsl:param name="elements" as="element()*"/>
<xsl:param name="level" as="xs:integer"/>
<xsl:for-each-group select="$elements" group-starting-with="p[@style eq concat('TRH', $level)]">
<xsl:choose>
<xsl:when test="self::p[@style eq 'TRH2']">
<chapter> <!--level="{$level}"-->
<title><xsl:apply-templates select="node()"/></title>
<xsl:sequence select="mf:group(current-group() except ., $level 1)"/>
</chapter>
</xsl:when>
<xsl:when test="self::p[@style eq 'TRH4']">
<division> <!--level="{$level}"-->
<title><xsl:apply-templates select="node()"/></title>
<xsl:sequence select="mf:group(current-group() except ., $level 1)"/>
</division>
</xsl:when>
<xsl:when test="self::p[@style eq 'TRH5']">
<subdivision> <!--level="{$level}"-->
<title><xsl:apply-templates select="node()"/></title>
<xsl:sequence select="mf:group(current-group() except ., $level 1)"/>
</subdivision>
</xsl:when>
<xsl:when test="self::p[@style eq 'TRH6']">
<arabicSubdivision> <!--level="{$level}"-->
<title><xsl:apply-templates select="node()"/></title>
<xsl:sequence select="mf:group(current-group() except ., $level 1)"/>
</arabicSubdivision>
</xsl:when>
<xsl:when test="self::p[@style eq 'TRH7']">
<section> <!--level="{$level}"-->
<title><xsl:apply-templates select="node()"/></title>
<xsl:sequence select="mf:group(current-group() except ., $level 1)"/>
</section>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*, node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<paragraph><xsl:apply-templates/></paragraph>
</xsl:template>
<xsl:template match="text">
<xsl:sequence select="mf:group(*, 2)"/>
</xsl:template>
<xsl:template match="r">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
電流輸出:
<?xml version="1.0" encoding="UTF-8"?>
<chapter>
<title>Test scenario 1 (TRH2) /chapter/title</title>
<section>
<title>Test Scenario 2 (TRH7) /chapter/section/title</title>
<paragraph>Test Scenario 3 (TRNormal) /chapter/section/paragraph </paragraph>
<table>a</table>
<division>
<title>Test Scenario 4 (TRH4) /chapter/division/title</title>
<section>
<title>Test Scenario 5 (TRH7) /chapter/division/section/title</title>
<paragraph>Test Scenario 6 (TRNormal) /chapter/division/section/paragraph </paragraph>
</section>
<subdivision>
<title>Test Scenario 7 (TRH5) /chapter/division/subdivision/title</title>
<arabicSubdivision>
<title>Test Scenario 8 (TRH6) /chapter/division/subdivision/arabicSubdivision/title</title>
<section>
<title>Test Scenario 9 (TRH7) /chapter/division/subdivision/arabicSubdivision/section/title</title>
<paragraph>Test Scenario 10 (TRNormal)
/chapter/division/subdivision/arabicSubdivision/section/paragraph </paragraph>
<paragraph>Test 1 </paragraph>
<paragraph>Test 2 </paragraph>
</section>
</arabicSubdivision>
</subdivision>
</division>
</section>
</chapter>
期望輸出:
<?xml version="1.0" encoding="UTF-8"?>
<chapter>
<title>Test scenario 1 (TRH2) /chapter/title</title>
<section>
<title>Test Scenario 2 (TRH7) /chapter/section/title</title>
<paragraph>Test Scenario 3 (TRNormal) /chapter/section/paragraph </paragraph>
<table>a</table>
</section>
<division>
<title>Test Scenario 4 (TRH4) /chapter/division/title</title>
<section>
<title>Test Scenario 5 (TRH7) /chapter/division/section/title</title>
<paragraph>Test Scenario 6 (TRNormal) /chapter/division/section/paragraph </paragraph>
</section>
<subdivision>
<title>Test Scenario 7 (TRH5) /chapter/division/subdivision/title</title>
<arabicSubdivision>
<title>Test Scenario 8 (TRH6) /chapter/division/subdivision/arabicSubdivision/title</title>
<section>
<title>Test Scenario 9 (TRH7) /chapter/division/subdivision/arabicSubdivision/section/title</title>
<paragraph>Test Scenario 10 (TRNormal)
/chapter/division/subdivision/arabicSubdivision/section/paragraph </paragraph>
<paragraph>Test 1 </paragraph>
<paragraph>Test 2 </paragraph>
</section>
</arabicSubdivision>
</subdivision>
</division>
</chapter>
uj5u.com熱心網友回復:
最近有一個類似的問題Not able to do XSLT transformation with grouping,我向其發布了答案https://stackoverflow.com/a/70662849/252228,以使其對您的輸入起作用,其中包含與您的p元素混合的table元素需要鍵入函式引數element()*而不是element(p)*:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
xmlns:mf="http://example.com/mf">
<xsl:param name="xml-levels-param">
<levels>
<level key="TRH2">
<container>chapter</container>
<content>title</content>
</level>
<level key="TRH4">
<container>division</container>
<content>title</content>
</level>
<level key="TRH5">
<container>subdivision</container>
<content>title</content>
</level>
<level key="TRH6">
<container>arabicSubdivision</container>
<content>title</content>
</level>
<level key="TRH7">
<container>section</container>
<content>title</content>
</level>
</levels>
</xsl:param>
<xsl:variable name="xml-levels" select="$xml-levels-param/levels/level"/>
<xsl:function name="mf:group-xml" as="node()*">
<xsl:param name="elements" as="element()*"/>
<xsl:param name="level" as="xs:integer"/>
<xsl:for-each-group select="$elements" group-starting-with="p[@style = $xml-levels[$level]/@key]">
<xsl:choose>
<xsl:when test="self::p[@style = $xml-levels[$level]/@key]">
<xsl:element name="{$xml-levels[$level]/container}">
<xsl:element name="{$xml-levels[$level]/content}">
<xsl:value-of select="r"/>
</xsl:element>
<xsl:choose>
<xsl:when test="exists($xml-levels[$level 1])">
<xsl:sequence select="mf:group-xml(current-group()[position() gt 1], $level 1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()[position() gt 1]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="exists($xml-levels[$level 1])">
<xsl:sequence select="mf:group-xml(current-group(), $level 1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="text">
<xsl:sequence select="mf:group-xml(*, 1)"/>
</xsl:template>
<xsl:template match="p[@style = 'TRNormal']">
<paragraphp>
<xsl:apply-templates select="r/node()"/>
</paragraphp>
</xsl:template>
<xsl:template match="p[not(node())]"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415030.html
標籤:
