【发布时间】:2018-11-27 08:19:30
【问题描述】:
背景 我有一个 schematron 验证和 xslt 到 pdf 的翻译。
问题 我想检查 xml 标记中的内容是否存在于外部文件的有效代码列表中。只有外部文件中的属性值是有趣的。节点值在另一个样式表中用于其他目的。
<xsl:template match="//medicalFieldcode" priority="1000" mode="M4">
<svrl:fired-rule xmlns:svrl="http://purl.oclc.org/dsdl/svrl" context="//medicalFieldcode"/>
<xsl:variable name="MVOcodes" select="document('verksamhetskodlista.xml')"/>
<!--ASSERT -->
<xsl:choose>
<xsl:when test="boolean($MVOcodes/module/document-merge/g-funcs/g[contains(@name,.)])"/>
<xsl:otherwise>
<svrl:failed-assert xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
test="boolean($MVOcodes/module/document-merge/g-funcs/g[contains(@name,.)])">
<xsl:attribute name="id">R43</xsl:attribute>
<xsl:attribute name="location">
<xsl:apply-templates select="." mode="schematron-select-full-path"/>
</xsl:attribute>
<svrl:text>MedicalFieldCode must be in the valid list of codes.</svrl:text>
</svrl:failed-assert>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="*|comment()|processing-instruction()" mode="M4"/>
</xsl:template>
外部文件 (verksamhetskodlista.xml) 具有以下结构:
<module xmlns:mmx="http://funx" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg" xmlns:fnc="http://funx/fnc" xmlns:att="http://funx/att" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<document-merge>
<g-funcs>
<g name="003">value1</g>
<g name="009">another value</g>
<g name="011">yet another value</g>
<!-- ... -->
</g-funcs>
</document-merge>
从示例 xml 中提取:
<ProcessClaimSpecification xmlns="urn:riv:financial:billing:claim:ProcessClaimSpecificationResponder:1">
<s01:claimSpecification xmlns="urn:riv:financial:billing:claim:1" xmlns:s01="urn:riv:financial:billing:claim:ProcessClaimSpecificationResponder:1">
<healthCareServicesSpecificationLine>
<healthcarePerformed>
<!-- a bunch of content -->
<activity>
<medicalFieldCode>000</medicalFieldCode>
</activity>
</healthcarePerformed>
</healthCareServicesSpecificationLine>
</s01:claimSpecification>
我需要使用 xpath 来验证这一点。 我试过应用这个contains instruction,运气不好,我一直在寻找,但无济于事。
问题在于它总是呈现为真,即使使用不在代码列表中的 000 也是如此。 我已经尝试了许多不同的 xpath 包含的重组品种,但效果并不令人满意。我不确定从正确的角度来这里。我使用的是 xslt 2.0,所以变量应该包含一个节点集。
除其他外,我已经尝试过这些 xpath 表达式
boolean($MVOcodes/module/document-merge/g-funcs/g[contains(@name,.)])
contains($MVOcodes/module/document-merge/g-funcs/g/@name, .)
我已经好几天没去那里了,请帮忙? 外部文件也在另一个 xsl 文档中使用,这就是它具有这种结构的原因。我想如果我重复使用它,它的维护就更少了。
【问题讨论】:
-
我认为你想比较
g[contains(@name, current()]而不是g[contains(@name,.)]。您尝试中的点.指的是您放置谓词的g元素。 -
谢谢!您是对的,但是,即使有效属性值列表中不存在该值,验证仍然会进行。 xpath 表达式中是否存在一些我没有发现的明显逻辑错误?
-
恐怕我还没有理解应该进行的那种验证,不清楚。尝试将问题减少到最小但完整的 XSLT 和 XML 示例以及想要的输出和当前输出,以允许其他人重现该问题。鉴于至少您的一些 XML 示例使用各种名称空间,因此根本不清楚您的 XSLT 匹配模式匹配什么以及您的 XPath 表达式选择什么,而没有看到任何带有名称空间声明(和
xpath-default-namespace声明?)的完整 XSLT 示例。
标签: xml xslt schematron