XSLT 应用模板和字符串操作

发布于 2025-01-01 02:21:28 字数 1106 浏览 2 评论 0原文

我有一些像这样的 XML:

    <subsection number="5">
       <p>
         (5) The <link path="123">Secretary of State</link> shall appoint such as....
      </p>
    </subsection>

我无法更改 XML,我需要删除段落开头的 (5) 并使用父标记中的 number 属性来创建带有适当标记的新段落编号

<xsl:template match="subsection/p">
        <xsl:variable name="number">
            <xsl:text>(</xsl:text>
            <xsl:value-of select="../@number"/>
            <xsl:text>)</xsl:text>
       </xsl:variable>
       <xsl:variable name="copy">
            <xsl:value-of select="."/>
       </xsl:variable>
       <p>
          <span class="indent">
            <xsl:value-of select="$number" />
          </span>
          <span class="copy">
            <xsl:value-of select="substring-after($copy, $number)" />
          </span>
       </p>
</xsl:template>

:问题是该段落的其余部分可能包含更多需要转换的 XML,例如示例中的链接标记。

使用 substring-after 函数后,我不知道如何将模板应用于此。

I have some XML like so:

    <subsection number="5">
       <p>
         (5) The <link path="123">Secretary of State</link> shall appoint such as....
      </p>
    </subsection>

I can't change the XML and I need to strip out the (5) at the start of the paragraph and use the number attribute in the parent tag to create a new paragraph number with appropriate markup:

<xsl:template match="subsection/p">
        <xsl:variable name="number">
            <xsl:text>(</xsl:text>
            <xsl:value-of select="../@number"/>
            <xsl:text>)</xsl:text>
       </xsl:variable>
       <xsl:variable name="copy">
            <xsl:value-of select="."/>
       </xsl:variable>
       <p>
          <span class="indent">
            <xsl:value-of select="$number" />
          </span>
          <span class="copy">
            <xsl:value-of select="substring-after($copy, $number)" />
          </span>
       </p>
</xsl:template>

The problem is the rest of the paragraph may contain more XML that needs to be transformed, such as the link tag in the example.

I don't know how to apply templates to this once I've used the substring-after function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书信已泛黄 2025-01-08 02:21:28

一种显式方法是将 subsection/p 元素的第一个文本子元素与所有其他子元素分开处理。出于演示目的,我还添加了一个用于将 link 元素转换为 a 元素的模板。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="subsection/p/text()[1]">
        <xsl:value-of select="concat('(', ../../@number, ')')"/>
    </xsl:template>
    <xsl:template match="subsection/p">
        <p>
            <span class="indent">
                <xsl:apply-templates select="text()[1]"/>
            </span>
            <span class="copy">
                <xsl:apply-templates select="*|text()[not(position()=1)]"/>
            </span>
        </p>
    </xsl:template>
    <xsl:template match="subsection/p/link">
        <a href="{@path}"><xsl:value-of select="."/></a>
    </xsl:template>
</xsl:stylesheet>

该样式表产生以下输出:

<p><span class="indent">(5)</span><span class="copy">
<a href="123">Secretary of State</a>shall appoint such as....</span></p>

An explicit approach is to handle the first text child of subsection/p elements separately from all other children. For demonstration purposes, I've also added a template for converting link elements into a elements.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="subsection/p/text()[1]">
        <xsl:value-of select="concat('(', ../../@number, ')')"/>
    </xsl:template>
    <xsl:template match="subsection/p">
        <p>
            <span class="indent">
                <xsl:apply-templates select="text()[1]"/>
            </span>
            <span class="copy">
                <xsl:apply-templates select="*|text()[not(position()=1)]"/>
            </span>
        </p>
    </xsl:template>
    <xsl:template match="subsection/p/link">
        <a href="{@path}"><xsl:value-of select="."/></a>
    </xsl:template>
</xsl:stylesheet>

This stylesheet produces the following output:

<p><span class="indent">(5)</span><span class="copy">
<a href="123">Secretary of State</a>shall appoint such as....</span></p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文