xml - 需要帮助使用 XSLT 模板转换测量值
问题描述
所以我已经尝试将这些数据制作成模板以将厘米转换为英寸,它也必须四舍五入。我必须通过模板来完成,不能在主 xslt 中完成(template='/' 它必须是命名模板)。这是为了上课。以下是一小段数据:
<org.openmrs.module.webservices.rest.SimpleObject
serialization="custom">
<unserializable-parents/>
<map>
<default>
<loadFactor>0.75</loadFactor>
<threshold>12</threshold>
</default>
<int>16</int>
<int>3</int>
<string>uuid</string>
<string>81f182fa-51d0-4790-87c2-912b3e011221</string>
<string>display</string>
<string>Height (cm): 156.0</string>
<string>links</string>
<list>
<org.openmrs.module.webservices.rest.web.Hyperlink>
<rel>self</rel>
<uri>http://b581.site/openmrs/ws/rest/v1/obs/81f182fa-51d0-4790-87c2-912b3e011221</uri>
</org.openmrs.module.webservices.rest.web.Hyperlink>
</list>
</map>
这是我迄今为止尝试过的:
> <xsl:call-template name="Bologna">
<xsl:with-param name= 'rounded' select='substring(./string[4],12,5)'/>
</xsl:call-template>
如果我不明白这一点,我可能会失败。我遇到了一位不愿教书的糟糕老师;如果您知道任何可以帮助我的资源,我将不胜感激。
<xsl:template match="/">
<data>
<extractor>
<name>Greg API Bushyeager</name>
</extractor>
<query> <method>GET</method>
<url>
<address>https://b581.site/openmrs/ws/rest/v1/</address>
<queryParameters>
<parameter key="v" value="full"/>
<parameter key="limit" value="500"/>
<parameter key="q" value="API"/>
</queryParameters> </url>
<headers type="nonHidden">
<header key="accept" value="application/xml"/>
</headers>
</query>
<xsl:for-each select="//string[(text() = '5090AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')]/../../..">
<obs uuid="5090AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA">
<person>
<xsl:value-of
select='./org.openmrs.module.webservices.rest.SimpleObject[2]/map/string[4]'/>
</person>
<originaldatetime>
<xsl:value-of select="./string[8]"/>
</originaldatetime>
<height>
<xsl:call-template name="cm-to-inch"/>
<Units>In</Units>
</height>
</obs>
</xsl:for-each>
</data>
</xsl:template>
我怎么能把你提供的模板调用到这个模板中,它只显示测量值。小新
Idc 如果单位在那里,我总是可以把它们扔进去。
解决方案
如果我不得不猜测你的任务的性质,我认为目的是创建一个通用模板,它将以厘米为单位的测量值转换为英寸。类似于可以从任何上下文调用并且没有自己的上下文的函数的东西:
<xsl:template name="cm-to-inch">
<xsl:param name="cm">
<xsl:value-of select="$cm div 2.54"/>
</xsl:template>
然后您将从另一个模板调用它,并将节点的值作为参数。例如:
XML
<object>
<name>box</name>
<length unit="cm">48</length>
<width unit="cm">20</width>
<height unit="cm">36</height>
<weight unit="kg">1.5</weight>
</object>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@unit='cm']">
<xsl:copy>
<xsl:attribute name="unit">in</xsl:attribute>
<xsl:call-template name="cm-to-inch">
<xsl:with-param name="cm" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="cm-to-inch">
<xsl:param name="cm"/>
<xsl:value-of select="$cm div 2.54"/>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<object>
<name>box</name>
<length unit="in">18.8976377952756</length>
<width unit="in">7.874015748031496</width>
<height unit="in">14.1732283464567</height>
<weight unit="kg">1.5</weight>
</object>
它也必须四舍五入。
好吧,这取决于您希望将其四舍五入的精度。XSLT 的round()
函数四舍五入到最接近的整数。如果您想要更高的精度 - 例如 2 个小数位 - 您需要执行以下操作:
<xsl:value-of select="round(100 * $cm div 2.54) div 100"/>
或者,您可以这样做:
<xsl:value-of select="format-number($cm div 2.54, '#.00')"/>
但这不是一回事,因为:
- 结果是一个可能包含尾随零的字符串;
- 根据处理器的不同,舍入规则可能会有所不同。
推荐阅读
- typescript - io-ts: 仅指一种类型,但在这里用作值
- cmake - Cmake 配置不完整,出现错误 | Box2d / Linux
- android - 在Kotlin中查找外部列表的最后出现索引值
- postgresql - Postgres时间与时区比较
- string - 如果它存在于不区分大小写的字符串中,python 返回特定的子字符串
- flutter - Flutter 在启动时加载 json 数据
- ruby-on-rails-4 - 在 Rails 4 中,如何从控制台获取控制器方法的路径?
- r - 在 R Shiny 中,如何对数据框的指定列求和并将结果输出到表中?
- python-3.x - 从字典中的前一个元素中减去列表的每个元素
- python - 如何从 google ads api 报告中检索数据(geo_performance_report)