【问题标题】:Is there a way to reference characters within an XML element有没有办法在 XML 元素中引用字符
【发布时间】:2016-07-24 07:05:46
【问题描述】:

我有一个 XML 文档,其文档结构包含章节、段落、文章、章节、小节等元素。我想为这些元素中的部分文本提供意义。

例如,部分文本可能显示为“参考区域内的建筑物不得高于 25m。”。我想引用“25”并将其赋予“最大建筑高度”的含义,因此它是机器可解释的。我可以通过在文本中添加 XML 元素来做到这一点。例如:

<text>A building within the referenced area may no not be higher than <meaning type="max building height">25</meaning>m.</text>

我更愿意将含义与文本分开,如果可能的话,与原始 XML 文档分开。是否可以在另一个 XML 文档的元素中引用部分文本?

示例源文档:

<document>
    <chapter id="5"><text>Building regulations</text>
        <article id="102"><text>Build</text>
            <section id="1a">
                <text>A building within the referenced area may no not be higher than 25m.</text>
            </section>
        </article>
    </chapter>
</document>

示例意义文档:

<meaning>
    <reference type="max building height" ref="..." start="..." end="..." />
</meaning>

【问题讨论】:

  • 你能附上你的文件的sn-p吗?
  • 添加了一个简化的例子。
  • 我不明白,你引用一个文本“参考区域内的建筑物可能不高于25m”然后说'我要参考“10”',没有10 在引用的文本中。
  • 从文本中提取数据在任何语言中都不是一个好主意(当然,除非您没有其他选择)。建筑法规的第 5 章/第 102 条/第 1a 节是否已经暗示了最大建筑高度?如果是这样,那么围绕这些构建你的逻辑。
  • @MartinHonnen 对不起我的错误。已更正。

标签: xml


【解决方案1】:

我之前关于从文本字符串中提取具有上下文含义的值的方法仍然适用,但这里有几个示例可以帮助您,

我的(首选)方法基于这样一个事实,即建筑法规通常是精确定义的,并且应该从章节/文章/章节 ID 中知道最大高度。

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>


<xsl:template match="chapter[@id='5']/article[@id='102']/section[@id='1a']/text">
<text>
    <xsl:value-of select="substring-before(.,'25')"/>
    <meaning type="max building height">25</meaning>
    <xsl:value-of select="substring-after(.,'25')"/>
</text>
</xsl:template>

如果你不知道最大高度是“25”,需要提取它。

这个版本并不完美(因为从字符串中提取数字并不简单!)。例如,如果您需要提取带有小数部分的数字(例如 12.5),那么这种方法需要更多的工作,因为众所周知,句点 (.) 也用于结束句子。 还有很多其他情况可以打破它。

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text">
<text>
    <xsl:variable name="tokens" select="fn:tokenize(.,' ')"/>
    <xsl:for-each select="$tokens">
        <xsl:variable name="v" select="fn:translate(.,'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.','')"/>
        <xsl:choose>
            <xsl:when test="string(number($v)) != 'NaN' ">
                <xsl:value-of select="substring-before(.,$v)"/>
                <meaning type="max building height"><xsl:value-of select="$v"/></meaning>
                <xsl:value-of select="substring-after(.,$v)"/>
                <xsl:text> </xsl:text>          
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="."/>
                <xsl:text> </xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
    </text>
</xsl:template>

【讨论】:

  • @PhilpBlackburn 我同意你的观点,这将成为一个猜谜游戏,因此变得复杂。回到绘图板 :-) 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
相关资源
最近更新 更多