我有一个 xml 结构,看起来有点像这样:
<Page>
<Id>Page1</Id>
<Content>
<Header>This is the Header</Header>
<Body>This is the body</Body>
<Nested>
<NestedItem>
<Id>N1</Id>
<Content>This is a nested element</Content>
</NestedItem>
<NestedItem>
<Id>N2</Id>
<Content>This too is a nested element</Content>
</NestedItem>
</Nested>
</Content>
<Localizations>
<Localization>
<Locale>ES</Locale>
<Content>
<Header>Esta un caballo</Header>
<Body>Esta body</Body>
<Nested>
<NestedItem>
<Id>N2</Id>
<Content>Esta una element nestado</Content>
</NestedItem>
</Nested>
</Content>
<Localization>
</Localizations>
</Page>
在 xslt 转换之后,我将变量“ES”传递给其中,在本例中,我希望它看起来像这样:
<Page>
<Id>Page1</Id>
<Content>
<Header>Esta un caballo</Header>
<Body>Esta body</Body>
<Nested>
<NestedItem>
<Id>N1</Id>
<Content>This is a nested element</Content>
</NestedItem>
<NestedItem>
<Id>N2</Id>
<Content>Esta una element nestado</Content>
</NestedItem>
</Nested>
</Content>
</Page>
即,我想翻译本地化中具有相应元素的元素,但在有原始文本的地方保留原始文本。问题是内容元素的结构可能未知,因此我无法在 xsl 文件中使用特定的标记名称。
到目前为止,我已经想到了这个:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:param name="locale"></xsl:param>
<xsl:template match="/" name="foo">
<xsl:for-each select="./*">
<xsl:if test="name() != 'Localizations'">
<xsl:element name="{name()}">
<xsl:variable name="elementName" select="name()"/>
<xsl:variable name="elementId" select="Id"/>
<xsl:variable name="elementContent">
<xsl:value-of select="./text()" />
</xsl:variable>
<xsl:variable name="localContent">
<xsl:for-each select="./ancestor::Page[1]/Localizations/Localization">
<xsl:if test="./Locale = $locale">
<xsl:copy-of select="*[name()=$elementName]/*"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="$localContent"/>
<xsl:call-template name="foo"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
它可以正常输出 xml,但它会在内容标签中创建元素的重复项,并且只有西类牙语内容,其他标签保持为空。我在这件事上走的路正确吗?任何指示都会受到赞赏,xslt 指南很难找到,我正在尝试在这里自学......
最佳答案
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kContentByElName" match="Localization/Content/*"
use="name()"/>
<xsl:key name="kNestedById" match="Localization/Content/Nested/NestedItem"
use="Id"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/Content/*[not(self::Nested)]/text()">
<xsl:variable name="vTranslation"
select="key('kContentByElName', name(..))"/>
<xsl:value-of select="$vTranslation | self::node()[not($vTranslation)]"/>
</xsl:template>
<xsl:template match=
"NestedItem[not(ancestor::Localization)]/Content/text()">
<xsl:variable name="vTranslation"
select="key('kNestedById', ../../Id)/Content"/>
<xsl:value-of select="$vTranslation | self::node()[not($vTranslation)]"/>
</xsl:template>
<xsl:template match="Localizations"/>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<Page>
<Id>Page1</Id>
<Content>
<Header>This is the Header</Header>
<Body>This is the body</Body>
<Nested>
<NestedItem>
<Id>N1</Id>
<Content>This is a nested element</Content>
</NestedItem>
<NestedItem>
<Id>N2</Id>
<Content>This too is a nested element</Content>
</NestedItem>
</Nested>
</Content>
<Localizations>
<Localization>
<Locale>ES</Locale>
<Content>
<Header>Esta un caballo</Header>
<Body>Esta body</Body>
<Nested>
<NestedItem>
<Id>N2</Id>
<Content>Esta una element nestado</Content>
</NestedItem>
</Nested>
</Content>
</Localization>
</Localizations>
</Page>
产生想要的正确结果:
<Page>
<Id>Page1</Id>
<Content>
<Header>Esta un caballo</Header>
<Body>Esta body</Body>
<Nested>
<NestedItem>
<Id>N1</Id>
<Content>This is a nested element</Content>
</NestedItem>
<NestedItem>
<Id>N2</Id>
<Content>Esta una element nestado</Content>
</NestedItem>
</Nested>
</Content>
</Page>
请注意:
这是一个纯 XSLT 1.0 解决方案。
按键用于快速搜索。
所有节点均使用身份规则“按原样”复制。
与需要处理的节点相匹配的模板会覆盖身份规则。
只要没有找到翻译,就会保留原始文本。
关于xml - 如何编写 xsl 样式表来根据匹配标签的内容翻译文档的某些部分......之类的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4050128/