如何根据 'sec/@disp-level' 值在 'sec/title' 元素下生成格式化数字序列
HOW TO GENERATE THE Sequence of formating numbers UNDER THE 'sec/title' ELEMENT BASED ON 'sec/@disp-level' value
这里我们根据'sec/@disp-level'生成title元素下的数字序列,下面是定义的层级格式(谁能帮帮我):
- @disp-level='1' = I., II., III.等等
- @disp-level='2' = A., B., C. 等等
- @disp-level='3' = 1., 2., 3. 等等
- @disp-level='4' = a., b., c.等等
- @disp-level='5' = (1), (2), (3) 等等
示例:在这个例子中是罗马数字。需要继续。
I.何时可以启动程序
III. 管辖权和地点
注意 - 我们在每个部分下进行排序。
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<sec id="ceb_750004516689gl" disp-level="1" specific-use="15.1">
<title><named-content content-type="none">§15.1</named-content> I. WHEN PROCEEDINGS MAY BE INITIATED</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_1889931838483us" disp-level="1">
<title>III. JURISDICTION AND VENUE</title>
<sec id="ceb_623954004359ua" disp-level="2">
<title>A. Trusts Under Continuing Court Jurisdiction</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_9159513279045ff" disp-level="2">
<title>A. Pursuit of Claims</title>
<sec id="ceb_791030056239at" disp-level="3" specific-use="15.2">
<title><named-content content-type="none">§15.2</named-content> 1. Supervised Versus Unsupervised Trusts</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_825038660717of" disp-level="3" specific-use="15.3">
<title><named-content content-type="none">§15.3</named-content> 3. Removal From Continuing Court Jurisdiction</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
<sec id="ceb_2491751884245sa" disp-level="4" specific-use="15.3A" sec-type="A">
<title><named-content content-type="none">§15.3A</named-content> a. Mandatory Removal for Trust With Corporate Trustee</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_655906801656qm" disp-level="4" specific-use="15.3B" sec-type="B">
<title><named-content content-type="none">§15.3B</named-content> 2. Discretionary Removal for Trust With Individual Trustee</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
<sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35">
<title><named-content content-type="none">§15.35</named-content> (1) Consent or Affirmation by Beneficiaries</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35A" sec-type="A">
<title><named-content content-type="none">§15.35A</named-content> (1) Consent or Affirmation by Beneficiaries (Added Section 5)</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</sec>
</sec>
</sec>
</sec>
<sec id="ceb_541077295354wt" disp-level="1">
<title>VI. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</root>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<sec id="ceb_750004516689gl" disp-level="1" specific-use="15.1">
<title><named-content content-type="none">§15.1</named-content> I. WHEN PROCEEDINGS MAY BE INITIATED</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_1889931838483us" disp-level="1">
<title>II. JURISDICTION AND VENUE</title>
<sec id="ceb_623954004359ua" disp-level="2">
<title>A. Trusts Under Continuing Court Jurisdiction</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_9159513279045ff" disp-level="2">
<title>B. Pursuit of Claims</title>
<sec id="ceb_791030056239at" disp-level="3" specific-use="15.2">
<title><named-content content-type="none">§15.2</named-content> 1. Supervised Versus Unsupervised Trusts</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_825038660717of" disp-level="3" specific-use="15.3">
<title><named-content content-type="none">§15.3</named-content> 2. Removal From Continuing Court Jurisdiction</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
<sec id="ceb_2491751884245sa" disp-level="4" specific-use="15.3A" sec-type="A">
<title><named-content content-type="none">§15.3A</named-content> a. Mandatory Removal for Trust With Corporate Trustee</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_655906801656qm" disp-level="4" specific-use="15.3B" sec-type="B">
<title><named-content content-type="none">§15.3B</named-content> b. Discretionary Removal for Trust With Individual Trustee</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
<sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35">
<title><named-content content-type="none">§15.35</named-content> (1) Consent or Affirmation by Beneficiaries</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35A" sec-type="A">
<title><named-content content-type="none">§15.35A</named-content> (2) Consent or Affirmation by Beneficiaries (Added Section 5)</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</sec>
</sec>
</sec>
</sec>
<sec id="ceb_541077295354wt" disp-level="1">
<title>III. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</root>
XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我用 xmlstarlet 做了这个:
$ xmlstarlet sel -t -m //title -v "named-content" -o "," -v . -n input.xml
§15.1,§15.1 I. WHEN PROCEEDINGS MAY BE INITIATED
,III. JURISDICTION AND VENUE
,A. Trusts Under Continuing Court Jurisdiction
,A. Pursuit of Claims
§15.2,§15.2 1. Supervised Versus Unsupervised Trusts
§15.3,§15.3 3. Removal From Continuing Court Jurisdiction
§15.3A,§15.3A a. Mandatory Removal for Trust With Corporate Trustee
§15.3B,§15.3B 2. Discretionary Removal for Trust With Individual Trustee
§15.35,§15.35 (1) Consent or Affirmation by Beneficiaries
§15.35A,§15.35A (1) Consent or Affirmation by Beneficiaries (Added Section 5)
,VI. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES
和 output.xml 相同:
$ xmlstarlet sel -t -m //title -v "named-content" -o "," -v . -n output.xml
§15.1,§15.1 I. WHEN PROCEEDINGS MAY BE INITIATED
,II. JURISDICTION AND VENUE
,A. Trusts Under Continuing Court Jurisdiction
,B. Pursuit of Claims
§15.2,§15.2 1. Supervised Versus Unsupervised Trusts
§15.3,§15.3 2. Removal From Continuing Court Jurisdiction
§15.3A,§15.3A a. Mandatory Removal for Trust With Corporate Trustee
§15.3B,§15.3B b. Discretionary Removal for Trust With Individual Trustee
§15.35,§15.35 (1) Consent or Affirmation by Beneficiaries
§15.35A,§15.35A (2) Consent or Affirmation by Beneficiaries (Added Section 5)
,III. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES
是的,我看到您的输入和输出之间存在一些差异,但不清楚 'desired change' 是什么。
我认为您只是想将 xsl:number
与适当的 format
一起使用:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="formats" as="map(xs:string, xs:string)"
select="map { '1' : 'I.', '2' : 'A.', '3' : '1.', '4' : 'a.', '5' : '(1)' }"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="sec[@disp-level]/title/text()[1]">
<xsl:variable name="number" as="xs:string">
<xsl:number count="sec[@disp-level]" format="{$formats(current()/../../@disp-level)}"/>
</xsl:variable>
<xsl:value-of select="replace(., '^\s*\(?[A-Z0-9.]+\)?', $number)"/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/naZYrqt
这是 XSLT 3 使用 XPath 3.1 映射来存储从级别编号到级别格式的映射,但当然它可以在 XML 变量或早期版本的 XSLT 中的序列中建模:
<xsl:param name="formats" as="xs:string*"
select="'I.', 'A.', '1.', 'a.', '(1)'"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="sec[@disp-level]/title/text()[1]">
<xsl:variable name="number" as="xs:string">
<xsl:number count="sec[@disp-level]" format="{$formats[position() = current()/../../@disp-level]}"/>
</xsl:variable>
<xsl:value-of select="replace(., '^\s*\(?[A-Z0-9.]+\)?', $number)"/>
</xsl:template>
这里我们根据'sec/@disp-level'生成title元素下的数字序列,下面是定义的层级格式(谁能帮帮我):
- @disp-level='1' = I., II., III.等等
- @disp-level='2' = A., B., C. 等等
- @disp-level='3' = 1., 2., 3. 等等
- @disp-level='4' = a., b., c.等等
- @disp-level='5' = (1), (2), (3) 等等
示例:在这个例子中是罗马数字。需要继续。
I.何时可以启动程序
III. 管辖权和地点
注意 - 我们在每个部分下进行排序。
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<sec id="ceb_750004516689gl" disp-level="1" specific-use="15.1">
<title><named-content content-type="none">§15.1</named-content> I. WHEN PROCEEDINGS MAY BE INITIATED</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_1889931838483us" disp-level="1">
<title>III. JURISDICTION AND VENUE</title>
<sec id="ceb_623954004359ua" disp-level="2">
<title>A. Trusts Under Continuing Court Jurisdiction</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_9159513279045ff" disp-level="2">
<title>A. Pursuit of Claims</title>
<sec id="ceb_791030056239at" disp-level="3" specific-use="15.2">
<title><named-content content-type="none">§15.2</named-content> 1. Supervised Versus Unsupervised Trusts</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_825038660717of" disp-level="3" specific-use="15.3">
<title><named-content content-type="none">§15.3</named-content> 3. Removal From Continuing Court Jurisdiction</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
<sec id="ceb_2491751884245sa" disp-level="4" specific-use="15.3A" sec-type="A">
<title><named-content content-type="none">§15.3A</named-content> a. Mandatory Removal for Trust With Corporate Trustee</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_655906801656qm" disp-level="4" specific-use="15.3B" sec-type="B">
<title><named-content content-type="none">§15.3B</named-content> 2. Discretionary Removal for Trust With Individual Trustee</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
<sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35">
<title><named-content content-type="none">§15.35</named-content> (1) Consent or Affirmation by Beneficiaries</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35A" sec-type="A">
<title><named-content content-type="none">§15.35A</named-content> (1) Consent or Affirmation by Beneficiaries (Added Section 5)</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</sec>
</sec>
</sec>
</sec>
<sec id="ceb_541077295354wt" disp-level="1">
<title>VI. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</root>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<sec id="ceb_750004516689gl" disp-level="1" specific-use="15.1">
<title><named-content content-type="none">§15.1</named-content> I. WHEN PROCEEDINGS MAY BE INITIATED</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_1889931838483us" disp-level="1">
<title>II. JURISDICTION AND VENUE</title>
<sec id="ceb_623954004359ua" disp-level="2">
<title>A. Trusts Under Continuing Court Jurisdiction</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_9159513279045ff" disp-level="2">
<title>B. Pursuit of Claims</title>
<sec id="ceb_791030056239at" disp-level="3" specific-use="15.2">
<title><named-content content-type="none">§15.2</named-content> 1. Supervised Versus Unsupervised Trusts</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_825038660717of" disp-level="3" specific-use="15.3">
<title><named-content content-type="none">§15.3</named-content> 2. Removal From Continuing Court Jurisdiction</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
<sec id="ceb_2491751884245sa" disp-level="4" specific-use="15.3A" sec-type="A">
<title><named-content content-type="none">§15.3A</named-content> a. Mandatory Removal for Trust With Corporate Trustee</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_655906801656qm" disp-level="4" specific-use="15.3B" sec-type="B">
<title><named-content content-type="none">§15.3B</named-content> b. Discretionary Removal for Trust With Individual Trustee</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
<sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35">
<title><named-content content-type="none">§15.35</named-content> (1) Consent or Affirmation by Beneficiaries</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
<sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35A" sec-type="A">
<title><named-content content-type="none">§15.35A</named-content> (2) Consent or Affirmation by Beneficiaries (Added Section 5)</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</sec>
</sec>
</sec>
</sec>
<sec id="ceb_541077295354wt" disp-level="1">
<title>III. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES</title>
<p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</root>
XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我用 xmlstarlet 做了这个:
$ xmlstarlet sel -t -m //title -v "named-content" -o "," -v . -n input.xml
§15.1,§15.1 I. WHEN PROCEEDINGS MAY BE INITIATED
,III. JURISDICTION AND VENUE
,A. Trusts Under Continuing Court Jurisdiction
,A. Pursuit of Claims
§15.2,§15.2 1. Supervised Versus Unsupervised Trusts
§15.3,§15.3 3. Removal From Continuing Court Jurisdiction
§15.3A,§15.3A a. Mandatory Removal for Trust With Corporate Trustee
§15.3B,§15.3B 2. Discretionary Removal for Trust With Individual Trustee
§15.35,§15.35 (1) Consent or Affirmation by Beneficiaries
§15.35A,§15.35A (1) Consent or Affirmation by Beneficiaries (Added Section 5)
,VI. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES
和 output.xml 相同:
$ xmlstarlet sel -t -m //title -v "named-content" -o "," -v . -n output.xml
§15.1,§15.1 I. WHEN PROCEEDINGS MAY BE INITIATED
,II. JURISDICTION AND VENUE
,A. Trusts Under Continuing Court Jurisdiction
,B. Pursuit of Claims
§15.2,§15.2 1. Supervised Versus Unsupervised Trusts
§15.3,§15.3 2. Removal From Continuing Court Jurisdiction
§15.3A,§15.3A a. Mandatory Removal for Trust With Corporate Trustee
§15.3B,§15.3B b. Discretionary Removal for Trust With Individual Trustee
§15.35,§15.35 (1) Consent or Affirmation by Beneficiaries
§15.35A,§15.35A (2) Consent or Affirmation by Beneficiaries (Added Section 5)
,III. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES
是的,我看到您的输入和输出之间存在一些差异,但不清楚 'desired change' 是什么。
我认为您只是想将 xsl:number
与适当的 format
一起使用:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="formats" as="map(xs:string, xs:string)"
select="map { '1' : 'I.', '2' : 'A.', '3' : '1.', '4' : 'a.', '5' : '(1)' }"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="sec[@disp-level]/title/text()[1]">
<xsl:variable name="number" as="xs:string">
<xsl:number count="sec[@disp-level]" format="{$formats(current()/../../@disp-level)}"/>
</xsl:variable>
<xsl:value-of select="replace(., '^\s*\(?[A-Z0-9.]+\)?', $number)"/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/naZYrqt
这是 XSLT 3 使用 XPath 3.1 映射来存储从级别编号到级别格式的映射,但当然它可以在 XML 变量或早期版本的 XSLT 中的序列中建模:
<xsl:param name="formats" as="xs:string*"
select="'I.', 'A.', '1.', 'a.', '(1)'"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="sec[@disp-level]/title/text()[1]">
<xsl:variable name="number" as="xs:string">
<xsl:number count="sec[@disp-level]" format="{$formats[position() = current()/../../@disp-level]}"/>
</xsl:variable>
<xsl:value-of select="replace(., '^\s*\(?[A-Z0-9.]+\)?', $number)"/>
</xsl:template>