繁体   English   中英

XSLT1.0:替换 XML 中属性出现的子环值

[英]XSLT1.0: Replace subsring value of occurence of attribute in XML

我有 XML 文件(ISO 19115 元数据 XML 文件),我想更改整个 XML 中代码列表属性的部分值。 URL for whole XML file https://rpi.gov.sk/rpi_csw/service.svc/get?request=getrecordbyid&service=csw&version=2.0.2&outputSchema=http://www.isotc211.org/2005/gmd&resulttype=results&elementsetname=full&id =https://data.gov.sk/set/rpi/gmd/00626031/170123074833

我想为所有出现替换部分代码列表值:

示例:输入:

 <gmd:hierarchyLevel>
    <gmd:MD_ScopeCode codeList="https://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml#MD_ScopeCode" codeListValue="service">service</gmd:MD_ScopeCode>
</gmd:hierarchyLevel>

预期 OUTPUT:

<gmd:hierarchyLevel>
    <gmd:MD_ScopeCode codeList="http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#MD_ScopeCode" codeListValue="service">service</gmd:MD_ScopeCode>
</gmd:hierarchyLevel>

我试过这个:

<xsl:param name="findText" select="'https://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml'" />
<xsl:param name="replaceText" select="'http://standards.iso.org/iso/19139/resources/gmxCodelists.xml'" />

<xsl:template match="@*">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="." />
        <xsl:with-param name="replace" select="$findText" />
        <xsl:with-param name="by" select="$replaceText" />
    </xsl:call-template>
</xsl:template>

<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text"
                    select="substring-after($text,$replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

但结果不是我所期望的。 替换是可以的,但它不保留元素/节点 - 替换的结果不是属性。 结构和结果是:

<gmd:hierarchyLevel>
    <gmd:MD_ScopeCode>http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#MD_ScopeCodeserviceservice</gmd:MD_ScopeCode>
</gmd:hierarchyLevel>

我怎样才能保持元素结构?

非常感谢

你不能简单地做:

<xsl:template match="@codeList">
    <xsl:attribute name="codeList">
        <xsl:text>http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#</xsl:text>
        <xsl:value-of select="substring-after(., '#')"/>
    </xsl:attribute>    
</xsl:template>

要将其限制为具有指定 URL 的codelist属性,您可以更改:

<xsl:template match="@codeList">

至:

<xsl:template match="@codeList[starts-with(., 'https://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml')">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM