注释 <xref> 标签并保持文本为纯文本

问题描述 投票:0回答:1

如果

xref
属性值与
xref/@href
属性值不匹配,我想注释
section/@id
标签并将文本保持为普通文本。

我已经创建了 xsl 代码来注释

xref
标签,但无法获得预期结果。

请查看预期结果并提出建议

提前致谢!

输入:

<?xml version="1.0" encoding="UTF-8"?>
<book id="book_id">
    <title>Generally Accepted Accounting Principles</title>
    <chapter id="chapter_id" role="ls_level_2">
        <chapterinfo>
            <titleabbrev>Chapter_abb</titleabbrev>
            <title>Chapter_title</title>
        </chapterinfo>
        <section id="aag-dep1_01">
            <para>text here</para>
            <para>text here</para>
            <para>containing auditing <xref href="fir_56_10">some text here</xref> guidance related to generally accepted auditing standard</para>
            <para> the effective dates for FASB ASU No. 2018 <xref href="aag-dep1_1.01">some text here</xref></para>
            <section id="aag-dep1_1.01">
                <para>text here <xref href="fot_79_ut">some text here</xref></para>
                <para>text here</para>
                <para>text here<xref href="aag-dep1_01">some text here</xref></para>
            </section>
            <section id="aag-dep1_2.01">
                <para>text here</para>
                <para>text here</para>
                <para>text here <xref href="aag-dep1_02">some text here</xref></para>
            </section>
        </section>
        <section id="aag-dep1_02">
            <para>text here</para>
            <para>text here</para>
            <para>ces, including engagements for entities in specia</para>
            <para>example, a large calendar-year public insurance en</para>
            <section id="aag-dep1_1.02">
                <para>text here</para>
                <para>text here</para>
                <para>text here <xref href="tih52_23">some text here</xref></para>
            </section>
        </section>
        <section id="aag-dep1_regulation_and_oversight">
            <para>text <xref href="aag-dep1_1.02">some text here</xref> here</para>
            <para>text here</para>
            <para>early application may do so as of the beginning</para>
            <para>Other auditing publications have no authoritative status;</para>
            <section id="aag-dep1_08">
                <para>text here <xref href="aag-dep1_regulation_and_oversight">some text here</xref></para>
                <para>text <xref href="nov1_22">some text here</xref> here</para>
                <para>text here</para>
            </section>
        </section>
    </chapter>
</book>

预期产量

<?xml version="1.0" encoding="UTF-8"?>
<book id="book_id">
    <title>Generally Accepted Accounting Principles</title>
    <chapter id="chapter_id" role="ls_level_2">
        <chapterinfo>
            <titleabbrev>Chapter_abb</titleabbrev>
            <title>Chapter_title</title>
        </chapterinfo>
        <section id="aag-dep1_01">
            <para>text here</para>
            <para>text here</para>
            <para>containing auditing <!--<xref href="fir_56_10">-->some text here<!--</xref>--> guidance related to generally accepted auditing standard</para>
            <para> the effective dates for FASB ASU No. 2018 <xref href="aag-dep1_1.01">some text here</xref></para>
            <section id="aag-dep1_1.01">
                <para>text <!--<xref href="fot_79_ut">-->some text here<!--</xref>--> here</para>
                <para>text here</para>
                <para>text here<xref href="aag-dep1_01">some text here</xref></para>
            </section>
            <section id="aag-dep1_2.01">
                <para>text here</para>
                <para>text here</para>
                <para>text here <xref href="aag-dep1_02">some text here</xref></para>
            </section>
        </section>
        <section id="aag-dep1_02">
            <para>text here</para>
            <para>text here</para>
            <para>ces, including engagements for entities in specia</para>
            <para>example, a large calendar-year public insurance en</para>
            <section id="aag-dep1_1.02">
                <para>text here</para>
                <para>text here</para>
                <para>text here <!--<xref href="tih52_23">-->some text here<!--</xref>--></para>
            </section>
        </section>
        <section id="aag-dep1_regulation_and_oversight">
            <para>text <xref href="aag-dep1_1.02">some text here</xref> here</para>
            <para>text here</para>
            <para>early application may do so as of the beginning</para>
            <para>Other auditing publications have no authoritative status;</para>
            <section id="aag-dep1_08">
                <para>text here <xref href="aag-dep1_regulation_and_oversight">some text here</xref></para>
                <para>text <!--<xref href="nov1_22">-->some text here<!--</xref>--> here</para>
                <para>text here</para>
            </section>
        </section>
    </chapter>
</book>

我的XSL代码:

<?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"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs xd"
    version="2.0">

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    
    <xsl:template match="xref">
        
        <xsl:choose>
            <xsl:when test="matches(@href, '^(aag_([0-9]+)([b]+))')">
                <xsl:comment>&lt;xref href="<xsl:value-of select="@href"/>"&gt;</xsl:comment><xsl:apply-templates/><xsl:comment>&lt;/xref&gt;</xsl:comment>
            </xsl:when>
            <xsl:otherwise>
                <xref>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates/>
                </xref>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    
</xsl:stylesheet>
xml xslt-2.0
1个回答
0
投票

使用隧道参数尝试此操作:

<?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"
  xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
  exclude-result-prefixes="xs xd"
  version="2.0">
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="chapter">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()">
        <!-- To speed up things we here collect all section/@id  and send them as tunnel param -->
        <xsl:with-param name="sectionIds" as="xs:string*" tunnel="yes" select="descendant::section/@id"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
    
  <xsl:template match="xref">
    <xsl:param name="sectionIds"  as="xs:string*" tunnel="yes"/>
    <xsl:choose>
      <xsl:when test="not(@href=$sectionIds)">
        <xsl:comment>&lt;xref href="<xsl:value-of select="@href"/>"&gt;</xsl:comment><xsl:apply-templates/><xsl:comment>&lt;/xref&gt;</xsl:comment>
      </xsl:when>
      <xsl:otherwise>
        <xref>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates/>
        </xref>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  
</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.