我想知道如何用缩写替换字符串。
我的XML如下所示
<concept reltype="CONTAINS" name="Left Ventricular Major Axis Diastolic Dimension, 4-chamber view" type="NUM">
<code meaning="Left Ventricular Major Axis Diastolic Dimension, 4-chamber view" value="18074-5" schema="LN" />
<measurement value="5.7585187646">
<code value="cm" schema="UCUM" />
</measurement>
<content>
<concept reltype="HAS ACQ CONTEXT" name="Image Mode" type="CODE">
<code meaning="Image Mode" value="G-0373" schema="SRT" />
<code meaning="2D mode" value="G-03A2" schema="SRT" />
</concept>
</content>
</concept>我从xml中选择了一些值,比如
<xsl:value-of select="concept/measurement/code/@value"/>现在我想要的是,我必须用厘米代替厘米。我有很多这样的话。我想有一个缩写和取代他们的xml。
我在这里看到了一个类似的例子。
Using a Map in XSL for expanding abbreviations
但它替换了节点文本,但我将文本作为属性。此外,如果在使用xsl:valueof select选择文本时能够找到并替换文本,而不是使用单独的xsl:template,对我来说会更好。请帮帮忙。我是第一次接触xslt。
发布于 2013-05-16 14:45:27
我已经创建了XSLT v "1.1“。对于缩写,我已经创建了XML文件,正如您所提到的:
Abbreviation.xml:
<Abbreviations>
<Abbreviation>
<Short>cm</Short>
<Full>centimeter</Full>
</Abbreviation>
<Abbreviation>
<Short>m</Short>
<Full>meter</Full>
</Abbreviation>
</Abbreviations>XSLT:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" />
<xsl:param name="AbbreviationDoc" select="document('Abbreviation.xml')"/>
<xsl:template match="/">
<xsl:call-template name="Convert">
<xsl:with-param name="present" select="concept/measurement/code/@value"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="Convert">
<xsl:param name="present"/>
<xsl:choose>
<xsl:when test="$AbbreviationDoc/Abbreviations/Abbreviation[Short = $present]">
<xsl:value-of select="$AbbreviationDoc/Abbreviations/Abbreviation[Short = $present]/Full"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$present"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>输入:
因为你已经给了<xsl:value-of select="concept/measurement/code/@value"/>
输出:
centimeter您只需要增强此Abbreviation.xml以保持缩写的简短和完整值,并使用传递当前值调用'Convert‘模板来获得所需的输出。
发布于 2013-05-16 16:53:14
下面是一个简短的版本:
但是对于xslt 1.0,需要节点集扩展。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="abbreviations_txt">
<abbreviation abbrev="cm" >centimeter</abbreviation>
<abbreviation abbrev="m" >meter</abbreviation>
</xsl:variable>
<xsl:variable name="abbreviations" select="exsl:node-set($abbreviations_txt)" />
<xsl:template match="/">
<xsl:apply-templates select="concept/measurement/code/@value" mode="abbrev_to_text"/>
</xsl:template>
<xsl:template match="* | @*" mode="abbrev_to_text">
<xsl:variable name="abbrev" select="." />
<xsl:variable name="long_text" select="$abbreviations//abbreviation[@abbrev = $abbrev]/text()" />
<xsl:value-of select="$long_text"/>
<xsl:if test="not ($long_text)">
<xsl:value-of select="$abbrev"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/16580202
复制相似问题