【问题标题】:adding the name of a xml document to the element IDs via XSLT通过 XSLT 将 xml 文档的名称添加到元素 ID
【发布时间】:2018-04-06 02:31:28
【问题描述】:

我有一个名为“document-a-f-52.xml”的 xml 文档,其内容如下。

输入:

<?xml version="1.0" encoding="UTF-8"?>
<ws>
    <w id="w_1">how</w>
    <w id="w_2">to</w>
    <w id="w_3">add</w>
    <w id="w_4">document</w>
    <w id="w_5">number</w>
    <w id="w_6">to</w>
    <w id="w_7">this</w>
    <w id="w_8">.</w>
</ws>

我想将文档名称“52”的数字部分(假设文档名称为 document-a-f-52.xml)添加到“w”元素的 id 中,如下所示:

 <?xml version="1.0" encoding="UTF-8"?>
 <ws>
    <w id="w_1_52">how</w>
    <w id="w_2_52">to</w>
    <w id="w_3_52">add</w>
    <w id="w_4_52">document</w>
    <w id="w_5_52">number</w>
    <w id="w_6_52">to</w>
    <w id="w_7_52">this</w>
    <w id="w_8_52">.</w>
</ws>

我想知道如何从文档名称中获取数字部分(最后两位数)并将其添加到“w”元素 id。

【问题讨论】:

  • 您使用 XSLT 2 还是 3?在那里,您可以访问 document-uribase-uri 等函数来访问文档的 URI(包括文件名)。

标签: xml xslt document


【解决方案1】:

根据您使用的 XSLT 处理器,您可以通过参数将 numeric 传递给您的 XSLT 样式表。

您的 XSLT 可能如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:param name="numeric" select="52" />

    <!-- identity template -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>  

    <!-- This template adds the 'numeric' value to the attribute 'id' -->
    <xsl:template match="@id">
        <xsl:attribute name="id">
            <xsl:value-of select="concat(.,'_',$numeric)" />
        </xsl:attribute>
    </xsl:template>  
</xsl:stylesheet>

numeric 参数可以由 XSLT 处理器传递给模板。
例如,xsltproc 您可以使用

xsltproc --stringparam numeric 100 a.xslt a.xml

得到类似的结果

<ws>
    <w id="w_1_100">how</w>
    <w id="w_2_100">to</w>
    <w id="w_3_100">add</w>
    <w id="w_4_100">document</w>
    <w id="w_5_100">number</w>
    <w id="w_6_100">to</w>
    <w id="w_7_100">this</w>
    <w id="w_8_100">.</w>
</ws>

其他 XSLT 处理器可能以不同方式处理参数传递。

【讨论】:

    【解决方案2】:

    使用 XSLT 3 或 2,您可以访问 document-uri()tokenize 以查找文件名作为最后一个标记并将任何非数字替换为空字符串,因此使用 XSLT 3 您只需要

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:math="http://www.w3.org/2005/xpath-functions/math"
        exclude-result-prefixes="xs math"
        version="3.0">
    
        <xsl:mode on-no-match="shallow-copy"/>
    
        <xsl:param name="suffix" as="xs:string" select="'_' || replace(tokenize(document-uri(), '/')[last()], '[^0-9]', '')"/>
    
        <xsl:template match="w/@id">
            <xsl:attribute name="{name()}" select=". || $suffix"/>
        </xsl:template>
    
    </xsl:stylesheet>
    

    使用 XSLT 2,您需要拼出上面使用的 xsl:mode on-no-match="shallow-copy" 作为身份转换模板,并使用 concat 函数而不是 || 连接运算符。

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 2015-10-08
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多