Xslt 使用ids从另一个xml更新xml
我必须从Xslt 使用ids从另一个xml更新xml,xslt,xslt-2.0,Xslt,Xslt 2.0,我必须从translation.xml中选择文本,并更新skeleton.xml(其中@xid=@id)节点。换句话说,如果translation.xml的@id与skeleton.xml的@xid匹配,文本内容应该放在skeleton.xml下(+如果有子元素表示文本内容(内联元素i,b,等等)还应进入相应的父元素:有关更多详细信息,请参见下面的示例: 注意:内联元素可以是skeleton.xml中的任何元素(它不限于b或i,因此应该是泛型的) skeleton.xml 这是第一段这是粗体这
translation.xml
中选择文本,并更新skeleton.xml
(其中@xid
=@id
)节点。换句话说,如果translation.xml
的@id
与skeleton.xml
的@xid
匹配,文本内容应该放在skeleton.xml
下(+如果有子元素表示文本内容(内联元素i
,b
,等等)还应进入相应的父元素:有关更多详细信息,请参见下面的示例:
注意:内联元素可以是skeleton.xml中的任何元素(它不限于b或i,因此应该是泛型的)
skeleton.xml
这是第一段这是粗体这是斜体其余部分粗体其余部分
这是第二段
这一点未变,第8段
这一点未变,第8段
translation.xml
这是第一段这是粗体这是斜体其余部分粗体其余部分
第一个秘密,第一个秘密,第二个秘密,第二个秘密,第三个秘密
这是第二段
第二个问题是什么
UpdatedSkeleton.xml
第一个秘密,第一个秘密,第二个秘密,第二个秘密,第三个秘密
第二个问题是什么
这一点未变,第8段
这一点未变,第8段
我正在尝试使用这段代码,但面临将内联内容的文本放置在正确位置的挑战:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:param name="translation" select="'file:/C:/Skeleton.xml'"></xsl:param>
<xsl:variable name="doc">
<xsl:copy-of select="doc($translation)"/>
</xsl:variable>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text">
<xsl:variable name="skelID" select="@xid"/>
<xsl:choose>
<xsl:when test="$doc//*[$skelID=@id]">
<xsl:apply-templates select="$doc//*[$skelID=@id]/target"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
您可以将
节点添加到测试块中,并使用
复制xid
属性。对于未修改的节点,您可以使用
复制完整树,包括属性:
<xsl:when test="$doc//*[$skelID=@id]">
<xsl:copy>
<xsl:attribute name="xid">
<xsl:value-of select="$skelID"/>
</xsl:attribute>
<xsl:apply-templates select="$doc//*[$skelID=@id]/target"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
我可能遗漏了一些内容,但这可能会解决您的大部分问题。谢谢您的解决方案,我很接近。但是如何在skeleton.xml中保留b和I标记的属性?
<root>
<para a="b" b="c">
<text xid="1">suum primum para <b xid="2" a="c" b="d">Et hoc confidens, <i xid="3" b="d" c="e">Hoc est, Te Deum</i> Reliqua autem audet,</b> reliqua autem verba haec</text>
</para>
<para><text xid="4">Hoc est secundum verba haec</text></para>
<para><text xid="5">This is unchanged para</text></para>
<para><text xid="6">This is unchanged para</text></para>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:param name="translation" select="'file:/C:/Skeleton.xml'"></xsl:param>
<xsl:variable name="doc">
<xsl:copy-of select="doc($translation)"/>
</xsl:variable>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text">
<xsl:variable name="skelID" select="@xid"/>
<xsl:choose>
<xsl:when test="$doc//*[$skelID=@id]">
<xsl:apply-templates select="$doc//*[$skelID=@id]/target"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<xsl:when test="$doc//*[$skelID=@id]">
<xsl:copy>
<xsl:attribute name="xid">
<xsl:value-of select="$skelID"/>
</xsl:attribute>
<xsl:apply-templates select="$doc//*[$skelID=@id]/target"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
<xsl:template match="target">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="g">
<xsl:element name="{@tagName}">
<xsl:attribute name="xid">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>