Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT:用缩写替换字符串_Xslt - Fatal编程技术网

XSLT:用缩写替换字符串

XSLT:用缩写替换字符串,xslt,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" v

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

我的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的缩写和取代他们

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

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

我已经创建了XSLT v“1.1”。对于缩写,我创建了XML文件,如您所述:

缩写.xml:

<Abbreviations>
  <Abbreviation>
    <Short>cm</Short>
    <Full>centimeter</Full>
  </Abbreviation>
  <Abbreviation>
    <Short>m</Short>
    <Full>meter</Full>
  </Abbreviation>
</Abbreviations>
<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>
centimeter

您只需增强这个缩写词.xml,以保持缩写词的简短和完整值,并通过传递当前值来调用“Convert”模板,以获得所需的输出。

这里有一个简短的版本:
-在xslt文件中使用缩写
-使用带模式的应用模板缩短使用时间

但是XSLT1.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>

厘米
米

您希望使用哪种XSLT版本的“1.0”或“2.0”?缩写列表应位于何处?第二个xml文件。与输入相同的xml文件,还是xslt文件?如果您使用的是xslt-1.0,您可以使用扩展节点集吗?我可以在xslt中使用缩写吗?我正在寻找1.0版。我觉得不错。如果我是对的,这也应该与xlst-1.0一起运行。(我不明白为什么使用parm变量也应该这样做)运行这个脚本有两种方法。首先,如果您使用的是Saxon6.5.5。保持xslt版本为“1.1”。第二,如果希望保持XSLT版本“1.0”,则必须实现XSLT扩展功能。关于param,我们定义了两个参数“AbbreviationDoc”(保留缩写.xml文件)和“present”,用于将xml值传递给模板“Convert”。我没有使用单独的xml文件。我在xslt中使用了带的变量。厘米