XSLT:用缩写替换字符串

时间:2013-05-16 06:13:10

标签: xslt

我想知道如何用缩写替换字符串。

我的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"/>

现在我想要的是,我必须用cm替换cm。我有这么多的话。我想有一个缩写的xml并替换它们。

我在这里看到了一个类似的例子。

Using a Map in XSL for expanding abbreviations

但它取代了节点文本,但我将text作为属性。此外,它对我来说会更好如果我可以在使用xsl:valueof select选择文本时找到并替换,而不是使用单独的xsl:template。请帮忙。我是xslt的新手。

2 个答案:

答案 0 :(得分:2)

我创建了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>

<强> INPUT:

正如您给出的<xsl:value-of select="concept/measurement/code/@value"/>

<强>输出:

centimeter

您只需要增强此Abbreviation.xml以保持缩写的短值和完整值,并通过传递当前值来调用“转换”模板以获得所需的输出。

答案 1 :(得分:1)

这里有一个更短的版本:
  - xslt文件中的缩写
  - 在模式下使用apply-templates,缩短使用时间。

但是xslt 1.0需要使用node-set扩展名。

<?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>