【发布时间】:2019-05-22 11:50:35
【问题描述】:
我正在尝试从外部 XML 文件交叉引用,但不是只比较一个键,而是询问是否存在一个字符串和其他字符串,以及是否存在来自外部文件的引用:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://www.tei-
c.org/ns/1.0"
xmlns="http://www.tei-c.org/ns/1.0" exclude-result-prefixes="xs t">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="ids"
select="document('instructions.xml')"/>
<xsl:key name="id" match="row" use="tokenize(normalize-space(elem[@name='Instruction']), ' ')"/>
<!-- identity transform -->
<xsl:template match="@* | node() | text() | *">
<xsl:copy>
<xsl:apply-templates select="@* | node() | text() | *"/>
</xsl:copy>
</xsl:template>
<xsl:template match="instruction">
<xsl:for-each select=".[contains(.,key('id', ., .))]">
<xsl:copy>
<xsl:attribute name="norm">
<xsl:value-of select="normalize-space(key('id', normalize-space(.), $ids)/elem[@name='Norm'])"/>
</xsl:attribute>
<xsl:apply-templates select="@* | node() | text() | *"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>
输入(外部文件):
<row>
<elem name="instruction">pour out</elem>
<elem name="norm">p1</elem>
</row>
输入(要注释的文件):
<ab type="recipe">
Bla bla
<instruction>pour the milk out</instruction> bla
</ab>
期望的输出:
<ab type="recipe">
Bla bla
<instruction norm="p1">pour the milk out</instruction> bla
</ab>
换句话说:外部 XML 文件中元素 <elem name="instruction">“pour”和“out”中的两个标记都需要包含在我的 XML 文件中的 <instruction>元素中。如果是,我想在外部文件中将 norm 属性设置为 <elem name="norm"> 的值。
非常感谢任何帮助!
【问题讨论】:
-
由于
elem name="instruction"元素包含您标记的单词列表,instruction元素中单词的顺序是否重要,即它是否必须以相同的顺序包含单词(@ 987654330@,out)? -
没有顺序根本不重要! “out blabla pour”也应该匹配。但是,我有这样的情况,我可以在输入数据中用另一个规范值“倒”。 "pour out --> p1", "pour --> p0" ..如果它实际上包含所有单词,它应该首先匹配。所以在这种情况下p1。如果是“倒牛奶”,应该是 p0。
标签: xslt xslt-2.0 cross-reference