我一直在研究,但没有找到任何与优化XSLT相关的东西。下面是我正在处理的代码片段,我想看看是否可以做些什么来帮助进行xslt转换。
<xsl:template match="a:OBR/*">
<xsl:choose>
<xsl:when test ="name() = 'OBR-10' and string-length(.) = 0">
<OBR-10>USER</OBR-10>
</xsl:when>
<xsl:when test ="name() = 'OBR-18'">
<OBR-18>
<xsl:value-of select ="//a:PV1/a:PV1-44"/>
</OBR-18>
</xsl:when>
<xsl:when test ="name() = 'OBR-19'">
<OBR-19>
<xsl:if test = "string-length(str:tokenize(../a:OBR-18,'^')[5]) > 0">
<xsl:value-of select ="str:tokenize(../a:OBR-18,'^')[5]"/>
</xsl:if>
</OBR-19>
</xsl:when>
<xsl:when test ="name() = 'OBR-33'">
<OBR-33>
<xsl:value-of select ="translate(../parent::a:ORC[1]/a:ORC-4,'^','~')"/>
</OBR-33>
</xsl:when>
<xsl:when test="name()='NTE'">
<NTE>
<xsl:apply-templates/>
</NTE>
</xsl:when>
<xsl:when test="name()='DG1'"/>
<!--<DG1>
<xsl:apply-templates/>
</DG1>
</xsl:when>-->
<xsl:when test="name()='OBX'">
<OBX>
<xsl:apply-templates/>
</OBX>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>发布于 2017-08-25 03:34:30
我建议编写如下代码
<xsl:template match="a:OBR/*">
<xsl:choose>
<xsl:when test ="name() = 'OBR-10' and string-length(.) = 0">
<OBR-10>USER</OBR-10>
</xsl:when>作为
<xsl:template match="a:OBR/OBR-10[string-length() = 0]">
<xsl:copy>USER</xsl:copy>
</xsl:template>或者也许
<xsl:template match="a:OBR/OBR-10[. = '']">
<xsl:copy>USER</xsl:copy>
</xsl:template>也就是说,编写按名称匹配每个元素模板,并在需要时使用谓词/条件来代替在*上匹配的奇怪方法,然后测试名称。我认为这不一定是一种优化(你必须用特定的实现来衡量),而是一种清晰和模块化的编码风格。
这个
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>将被编码为
<xsl:template match="a:OBR/*">
<xsl:copy-of select="."/>
</xsl:template>或者可能已经被设置为启动和保持处理的起点的身份转换模板所覆盖。
您必须在输入文档和XSLT中显示名称空间,以便提供关于名称空间的精确建议(可能是您想要/需要xsl:template match="a:OBR/a:OBR-10[string-length() = 0]")。
https://stackoverflow.com/questions/45868310
复制相似问题