Xml XSLT2.0:从元素创建子元素';通过已知的语义层次结构定义文本值
这个有点卡住了。数据以以下格式提供(非重要内容截取): …并通过XSLT 2.0转换将其作为父子元素关系放置为最终输出:Xml XSLT2.0:从元素创建子元素';通过已知的语义层次结构定义文本值,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,这个有点卡住了。数据以以下格式提供(非重要内容截取): …并通过XSLT 2.0转换将其作为父子元素关系放置为最终输出: <?xml version="1.0" encoding="UTF-8"?> <law> <!--SNIP--> <content> <section prefix="(1)"> <section prefix="(a)"&
<?xml version="1.0" encoding="UTF-8"?>
<law>
<!--SNIP-->
<content>
<section prefix="(1)">
<section prefix="(a)">The statutes ...
<section prefix="(b)">To ensure public ..:
<section prefix="(I)">Shall authorize ...;</section>
<section prefix="(II)">May authorize and ...:
<section prefix="(A)">Compact disks;</section>
<section prefix="(B)">On-line public ...;</section>
<section prefix="(C)">Electronic applications for ..;</section>
<section prefix="(D)">Electronic books or ...</section>
<section prefix="(E)">Other electronic products or formats;</section>
</section>
<section prefix="(III)">May, pursuant ...</section>
<section prefix="(IV)">Recognizes that ...</section>
</section>
</section>
<section prefix="(2)">
<section prefix="(a)">Any person, ...:
<section prefix="(I)">A statement specifying ...;</section>
<section prefix="(II)">A statement specifying ...;</section>
</section>
</section>
<section prefix="(3)">Level 1 node with no children</section>
</content>
</law>
法令。。。
为确保公众安全:
应授权。。。;
可授权并…:
光盘;
在线公共。。。;
电子申请。。;
电子书籍或。。。
其他电子产品或格式;
根据。。。
认识到。。。
任何人,…:
说明……的声明。。。;
说明……的声明。。。;
没有子节点的级别1节点
我能够根据内容的文本值标记结束的html编码的p标记,但不知道如何获得动态创建的元素来创建条件上的子元素
我的XSLT 2.0样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Content">
<!-- Work from the lowest index level with no children up -->
<xsl:apply-templates select=".//Index[@HasChildren=0]"/>
</xsl:template>
<xsl:template match="Index[@HasChildren=0]">
<law>
<structure>
<xsl:apply-templates select="Content"/>
</structure>
</law>
</xsl:template>
<!-- Template for Content element from originial -->
<xsl:template match="Content">
<content>
<!-- Loop through HTML encoded P tag endings -->
<xsl:for-each select="tokenize(.,'</p>')">
<!-- Set Token to a variable and remove P opening tags -->
<xsl:variable name="sectionText">
<xsl:value-of select="normalize-space(replace(current(),'<p>',''))"/>
</xsl:variable>
<!-- Output -->
<xsl:if test="string-length($sectionText)!=0">
<section>
<!-- Set the section element's prefix attribute (if exists) -->
<xsl:analyze-string select="$sectionText" regex="^(\(([\w]+)\)){{1,3}}">
<xsl:matching-substring >
<xsl:attribute name="prefix" select="." />
</xsl:matching-substring>
</xsl:analyze-string>
<!-- Set the section element's value -->
<xsl:value-of select="$sectionText"/>
</section>
</xsl:if>
</xsl:for-each>
</content>
</xsl:template>
</xsl:stylesheet>
<?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:mf="http://example.com/mf"
xmlns:d="data:,dpc"
exclude-result-prefixes="xs d mf">
<xsl:include href="htmlparse.xml"/>
<xsl:param name="patterns" as="element(pattern)*" xmlns="">
<pattern value="^\s*(\([0-9]+\))" group="1" next="1"/>
<pattern value="^\s*(\([0-9]+\))?\s*(\([a-z]\))" group="2" next="0"/>
<pattern value="^\s*(\(*(I|II|III|IV|V|VI|VII|VIII|IX|X|XI|XII|XIII)\))" group="1" next="0"/>
<pattern value="^\s*(\([A-Z]?\))" group="1" next="0"/>
</xsl:param>
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:function name="mf:group" as="element(section)*">
<xsl:param name="paragraphs" as="element(p)*"/>
<xsl:param name="patterns" as="element(pattern)*"/>
<xsl:variable name="pattern1" as="element(pattern)?" select="$patterns[1]"/>
<xsl:for-each-group select="$paragraphs" group-starting-with="p[matches(., $pattern1/@value)]">
<xsl:variable name="prefix" as="xs:string?">
<xsl:analyze-string select="." regex="{$pattern1/@value}">
<xsl:matching-substring>
<xsl:sequence select="string(regex-group(xs:integer($pattern1/@group)))"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<section prefix="{$prefix}">
<xsl:choose>
<xsl:when test="xs:boolean(xs:integer($pattern1/@next))">
<xsl:sequence select="mf:group(current-group(), $patterns[position() gt 1])"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()">
<xsl:with-param name="pattern" as="element(pattern)" select="$pattern1" tunnel="yes"/>
</xsl:apply-templates>
<xsl:sequence select="mf:group(current-group() except ., $patterns[position() gt 1])"/>
</xsl:otherwise>
</xsl:choose>
</section>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="/Content">
<!-- Work from the lowest index level with no children up -->
<xsl:apply-templates select=".//Index[@HasChildren=0]"/>
</xsl:template>
<xsl:template match="Index[@HasChildren=0]">
<law>
<structure>
<xsl:apply-templates select="Content"/>
</structure>
</law>
</xsl:template>
<!-- Template for Content element from originial -->
<xsl:template match="Content">
<content>
<xsl:sequence select="mf:group(d:htmlparse(., '', true())/*, $patterns)"/>
</content>
</xsl:template>
<xsl:template match="p/text()[1]">
<xsl:param name="pattern" as="element(pattern)" tunnel="yes"/>
<xsl:value-of select="replace(., $pattern/@value, '')"/>
</xsl:template>
</xsl:stylesheet>
…这让我走了这么远-在部分元素中没有语义层次结构:
<?xml version="1.0" encoding="UTF-8"?>
<law>
<structure>
<content>
<section prefix="(1)(a)">(1)(a)The statutes ...</section>
<section prefix="(b)">(b)To ensure public ..:</section>
<section prefix="(I)">(I)Shall authorize ...;</section>
<section prefix="(II)">(II)May authorize and ...:</section>
<section prefix="(A)">(A)Compact disks;</section>
<section prefix="(B)">(B)On-line public ...;</section>
<section prefix="(C)">(C)Electronic applications for ..;</section>
<section prefix="(D)">(D)Electronic books or ...</section>
<section prefix="(E)">(E)Other electronic products or formats;</section>
<section prefix="(III)">(III)May, pursuant ...</section>
<section prefix="(IV)">(IV)Recognizes that ...</section>
<section prefix="(2)(a)">(2)(a)Any person, ...:</section>
<section prefix="(I)">(I)A statement specifying ...;</section>
<section prefix="(II)">(II)A statement specifying ...;</section>
<section prefix="(3)">(3)Level 1 section with no children ...;</section>
</content>
</structure>
</law>
(1) (a)规约。。。
(b) 为确保公众安全:
(一) 应授权。。。;
(二) 可授权并…:
(A) 光盘;
(B) 在线公共。。。;
(C) 电子申请。。;
(D) 电子书籍或。。。
(E) 其他电子产品或格式;
(三) 根据。。。
(四) 认识到。。。
(2) (a)任何人,…:
(一) 说明……的声明。。。;
(二) 说明……的声明。。。;
(3) 无子级的1级段。。。;
由于XSLT 2.0样式表通过标记end p标记动态创建了部分元素,因此如何通过前缀属性动态地构建具有已知语义层次结构的父子关系
其他编程语言的经验为我指明了递归的方向,这是基于前缀上的标记化和逻辑,以其先前的嵌套前缀为基础的——我在v2.0(大约10多年前使用v1.0)中有限的XSLT知识,很难找到如何做到这一点的任何信息。我知道我可以用一个外部Python脚本进行解析,然后就可以完成了,但是为了可维护性,我尝试使用XSLT2.0样式表解决方案
非常感谢您的任何帮助,让我走上正确的轨道和/或解决方案。您已经解决了问题的一个棘手阶段,用以下元素创建了中间输出:
<section prefix="(1)(a)">text</section>
<section level="1" prefix="(1)(a)">text</section>
文本
我的下一步将是计算一个级别数,它看起来如下:
<section prefix="(1)(a)">text</section>
<section level="1" prefix="(1)(a)">text</section>
文本
计算级别数只是一个问题,看看前缀与几个正则表达式中的哪一个匹配:(1)给出级别1,(b)给出级别2,等等
一旦获得了级别编号,就可以使用本文中描述的递归位置分组:我对此进行了一些研究,并提出了以下样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Content">
<!-- Work from the lowest index level with no children up -->
<xsl:apply-templates select=".//Index[@HasChildren=0]"/>
</xsl:template>
<xsl:template match="Index[@HasChildren=0]">
<law>
<structure>
<xsl:apply-templates select="Content"/>
</structure>
</law>
</xsl:template>
<!-- Template for Content element from originial -->
<xsl:template match="Content">
<content>
<!-- Loop through HTML encoded P tag endings -->
<xsl:for-each select="tokenize(.,'</p>')">
<!-- Set Token to a variable and remove P opening tags -->
<xsl:variable name="sectionText">
<xsl:value-of select="normalize-space(replace(current(),'<p>',''))"/>
</xsl:variable>
<!-- Output -->
<xsl:if test="string-length($sectionText)!=0">
<section>
<!-- Set the section element's prefix attribute (if exists) -->
<xsl:analyze-string select="$sectionText" regex="^(\(([\w]+)\)){{1,3}}">
<xsl:matching-substring >
<xsl:attribute name="prefix" select="." />
</xsl:matching-substring>
</xsl:analyze-string>
<!-- Set the section element's value -->
<xsl:value-of select="$sectionText"/>
</section>
</xsl:if>
</xsl:for-each>
</content>
</xsl:template>
</xsl:stylesheet>
<?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:mf="http://example.com/mf"
xmlns:d="data:,dpc"
exclude-result-prefixes="xs d mf">
<xsl:include href="htmlparse.xml"/>
<xsl:param name="patterns" as="element(pattern)*" xmlns="">
<pattern value="^\s*(\([0-9]+\))" group="1" next="1"/>
<pattern value="^\s*(\([0-9]+\))?\s*(\([a-z]\))" group="2" next="0"/>
<pattern value="^\s*(\(*(I|II|III|IV|V|VI|VII|VIII|IX|X|XI|XII|XIII)\))" group="1" next="0"/>
<pattern value="^\s*(\([A-Z]?\))" group="1" next="0"/>
</xsl:param>
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:function name="mf:group" as="element(section)*">
<xsl:param name="paragraphs" as="element(p)*"/>
<xsl:param name="patterns" as="element(pattern)*"/>
<xsl:variable name="pattern1" as="element(pattern)?" select="$patterns[1]"/>
<xsl:for-each-group select="$paragraphs" group-starting-with="p[matches(., $pattern1/@value)]">
<xsl:variable name="prefix" as="xs:string?">
<xsl:analyze-string select="." regex="{$pattern1/@value}">
<xsl:matching-substring>
<xsl:sequence select="string(regex-group(xs:integer($pattern1/@group)))"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<section prefix="{$prefix}">
<xsl:choose>
<xsl:when test="xs:boolean(xs:integer($pattern1/@next))">
<xsl:sequence select="mf:group(current-group(), $patterns[position() gt 1])"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()">
<xsl:with-param name="pattern" as="element(pattern)" select="$pattern1" tunnel="yes"/>
</xsl:apply-templates>
<xsl:sequence select="mf:group(current-group() except ., $patterns[position() gt 1])"/>
</xsl:otherwise>
</xsl:choose>
</section>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="/Content">
<!-- Work from the lowest index level with no children up -->
<xsl:apply-templates select=".//Index[@HasChildren=0]"/>
</xsl:template>
<xsl:template match="Index[@HasChildren=0]">
<law>
<structure>
<xsl:apply-templates select="Content"/>
</structure>
</law>
</xsl:template>
<!-- Template for Content element from originial -->
<xsl:template match="Content">
<content>
<xsl:sequence select="mf:group(d:htmlparse(., '', true())/*, $patterns)"/>
</content>
</xsl:template>
<xsl:template match="p/text()[1]">
<xsl:param name="pattern" as="element(pattern)" tunnel="yes"/>
<xsl:value-of select="replace(., $pattern/@value, '')"/>
</xsl:template>
</xsl:stylesheet>
如果可以有超过13(XIII)个部分,则需要使用罗马数字的正则表达式模式编辑参数以列出更多数字,因为我目前只列出了包括XIII在内的数字
根据注释和编辑的输入示例,我对样式表进行了一些修改:
<?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:mf="http://example.com/mf"
xmlns:d="data:,dpc"
exclude-result-prefixes="xs d mf">
<xsl:include href="htmlparse.xml"/>
<xsl:param name="patterns" as="element(pattern)*" xmlns="">
<pattern value="^\s*(\([0-9]+\))" group="1" next="1"/>
<pattern value="^\s*(\([0-9]+\))?\s*(\([a-z]\))" group="2" next="0"/>
<pattern value="^\s*(\(*(I|II|III|IV|V|VI|VII|VIII|IX|X|XI|XII|XIII)\))" group="1" next="0"/>
<pattern value="^\s*(\([A-Z]?\))" group="1" next="0"/>
</xsl:param>
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:function name="mf:group" as="element(section)*">
<xsl:param name="paragraphs" as="element(p)*"/>
<xsl:param name="patterns" as="element(pattern)*"/>
<xsl:variable name="pattern1" as="element(pattern)?" select="$patterns[1]"/>
<xsl:for-each-group select="$paragraphs" group-starting-with="p[matches(., $pattern1/@value)]">
<xsl:variable name="prefix" as="xs:string?">
<xsl:analyze-string select="." regex="{$pattern1/@value}">
<xsl:matching-substring>
<xsl:sequence select="string(regex-group(xs:integer($pattern1/@group)))"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<section prefix="{$prefix}">
<xsl:choose>
<xsl:when test="xs:boolean(xs:integer($pattern1/@next)) and matches(., $patterns[2]/@value)">
<xsl:sequence select="mf:group(current-group(), $patterns[position() gt 1])"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()">
<xsl:with-param name="pattern" as="element(pattern)" select="$pattern1" tunnel="yes"/>
</xsl:apply-templates>
<xsl:sequence select="mf:group(current-group() except ., $patterns[position() gt 1])"/>
</xsl:otherwise>
</xsl:choose>
</section>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="/Content">
<!-- Work from the lowest index level with no children up -->
<xsl:apply-templates select=".//Index[@HasChildren=0]"/>
</xsl:template>
<xsl:template match="Index[@HasChildren=0]">
<law>
<structure>
<xsl:apply-templates select="Content"/>
</structure>
</law>
</xsl:template>
<!-- Template for Content element from originial -->
<xsl:template match="Content">
<content>
<xsl:sequence select="mf:group(d:htmlparse(., '', true())/*, $patterns)"/>
</content>
</xsl:template>
<xsl:template match="p/text()[1]">
<xsl:param name="pattern" as="element(pattern)" tunnel="yes"/>
<xsl:value-of select="replace(., $pattern/@value, '')"/>
</xsl:template>
</xsl:stylesheet>
现在它改变了
<?xml version="1.0" encoding="UTF-8"?>
<Content Type="Statutes">
<Indexes>
<!--SNIP-->
<Index Level="3" HasChildren="0">
<!--SNIP-->
<Content><p> (1)(a)The statutes ... </p><p> (b)To ensure public ..: </p><p>
(I)Shall authorize ...; </p><p> (II)May authorize and ...: </p><p> (A)Compact disks;
</p><p> (B)On-line public ...; </p><p> (C)Electronic applications for ..;
</p><p> (D)Electronic books or ... </p><p> (E)Other electronic products or formats;
</p><p> (III)May, pursuant ... </p><p> (IV)Recognizes that ... </p><p>
(2)(a)Any person, ...: </p><p> (I)A statement specifying ...; </p><p> (II)A statement
specifying ...; </p><p> (3)A statement
specifying ...; </p><p> (4)A statement
specifying ...; </p></Content>
</Index>
<!--SNIP-->
</Indexes>
</Content>
p(1)(a)章程/pp(b)确保公共安全。:/pp
(一) 应授权/pp(II)可授权和……:/pp(A)光盘;
/pp(B)在线公共/pp(C)电子应用程序:。。;
/pp(D)电子图书或/pp(E)其他电子产品或格式;
/pp(III)可根据/pp(IV)承认/聚丙烯
(2) (a)任何人,……:/pp(I)说明/pp(II)声明
具体说明/pp(3)声明
具体说明/pp(4)声明
具体说明/P
到
法令。。。
为确保公众……应授权。。。;
可授权和…:光盘;
在线公共。。。;
电子申请。。;
电子书籍或。。。
其他电子产品或格式;
根据。。。
认识到。。。