Schematron:混合类型元素中的元素仅与其他内容一起使用

时间:2014-02-06 08:23:57

标签: string function xslt sequence schematron

我的XML数据应如下所示:

<mixed_type_parent>

...text...

  <other_element1/>
  <other_element2/>

  <element_in_question>...some content...</element_in_question>

  <other_element2>...text...</other_element2>
  <other_element1>...text...</other_element1>

...text...
</mixed_type_parent>

我想确保使用Schematron是“element_in_question”可能只出现在“mixed_type_parent”中,如果在“element_in_question”之外有一些文本。 这意味着

<mixed_type_parent>
  <element_in_question>...some content...</element_in_question>
</mixed_type_parent>

是不允许的,应该会导致错误。

我试图在“mixed_type_parent”

中立即获取所有文本的字符串长度
string-length(replace(ancestor::mixed_type_parent[1]/text(),' ', ''))

但是,再一次,XPath中存在一个最烦人的错误:“不允许多个项目的序列作为replace()的第一个参数”

在XSLT中,我通过你能想到的最简单的函数解决了这个问题:

<xsl:function name="locfun:make_string">
 <xsl:param name="input_sequence"/>
 <xsl:value-of select="$input_sequence"/>
</xsl:function>

(在XPath中没有这样的内置函数,实在令人遗憾。)

但是如何在Schematron中使用此功能?我没有找到解决方案。

除此之外:除了“mixed_type_parent”之外,如何从“mixed_type_parent”的所有其他子项中获取所有文本?

2 个答案:

答案 0 :(得分:2)

试试这个:

string-join(ancestor::mixed_type_parent[1]/text(),'')=''

关于第二个问题:除了“mixed_type_parent”之外,如何从“mixed_type_parent”的所有其他子项中获取所有文本?

/mixed_type_parent/*[not(self::element_in_question)]/text()

答案 1 :(得分:0)

考虑这个输入XML,

<mixed_type_parent>
    <element_in_question>...some content...</element_in_question>
</mixed_type_parent>

应用此XSLT时:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

<xsl:strip-space elements="*"/>

    <xsl:template match="element_in_question">
        <xsl:choose>
            <xsl:when test="preceding-sibling::text() or following-sibling::text()">
                true
            </xsl:when>
            <xsl:otherwise>
                false
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

产生

false