XSLT1.0:替换XML中属性出现的子字符串值
我有一个XML文件(ISO 19115元数据XML文件),我想更改整个XML中代码列表属性的部分值。 整个XML文件的URL 我想替换所有事件的部分代码列表值: 例如: 输入:XSLT1.0:替换XML中属性出现的子字符串值,xml,xslt,replace,attributes,substring,Xml,Xslt,Replace,Attributes,Substring,我有一个XML文件(ISO 19115元数据XML文件),我想更改整个XML中代码列表属性的部分值。 整个XML文件的URL 我想替换所有事件的部分代码列表值: 例如: 输入: 服务 预期产出: <gmd:hierarchyLevel> <gmd:MD_ScopeCode codeList="http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#MD_ScopeCode" co
服务
预期产出:
<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>
http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#MD_ScopeCodeserviceservice
如何使元素保持结构
非常感谢您不能简单地:
<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>
致:
欢迎来到SO。它不是免费的代码编写服务;我们的想法是尝试一些东西,如果你被卡住了,我们可以帮助你。你想用什么替换哪一部分?您的示例并不构成规则。这也是一种方式,但我不需要替换所有的代码列表值,只需将代码列表替换为值“”@RadoslavChudý请参阅我的答案中的添加内容。非常感谢。
<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>
<xsl:template match="@codeList">
<xsl:template match="@codeList[starts-with(., 'https://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml')">