これに少し引っかかっています。データは次の形式で提供されます (重要でないコンテンツは省略されています)。
<?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>
セマンティック階層を含む要素Contentのテキスト値を取得する必要があります。
(1)
+-(a)
+-(I)
+-(A)
...最終的な出力として親子要素関係として XSLT 2.0 変換を介して配置します。
<?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>
コンテンツのテキスト値から 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"?>
<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>
Section要素は、終了 P タグをトークン化することによって XSLT 2.0 スタイルシートによって動的に作成されるため、 prefix属性 を介して既知のセマンティック階層を使用して動的に親子関係を構築するにはどうすればよいでしょうか?
他のプログラミング言語の経験から、ネストのための前のプレフィックスに対するプレフィックスのトークン化とロジックに基づく再帰の方向に私は導かれます - v2.0 での限られた XSLT 知識 (v1. 0 ほぼ 10 年以上前)。外部の Python スクリプトを使用して解析するだけで済むことはわかっていますが、保守性のために XSLT 2.0 スタイルシート ソリューションに固執しようとしています。
正しい軌道や解決策にたどり着くために、どんな助けでも大歓迎です。