首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写xsl样式表来根据匹配的tags...sort的内容翻译文档的各个部分?

如何编写xsl样式表来根据匹配的tags...sort的内容翻译文档的各个部分?
EN

Stack Overflow用户
提问于 2010-10-29 16:14:13
回答 3查看 1.2K关注 0票数 2

我有一个xml结构,看起来有点像这样:

代码语言:javascript
复制
<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“传入其中,在本例中,我希望它看起来像这样:

代码语言:javascript
复制
<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>

也就是说,我想翻译在本地化中有相应元素的元素,但在有原始文本的地方保留原始文本。问题是content-element的结构可能是未知的,所以我不能在xsl-file中使用特定的标记名。

到目前为止,我得出了以下结论:

代码语言:javascript
复制
<?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,但是它在content-tag中创建了元素的副本,并且只有西班牙语内容,其他的标签保持为空。在这一点上,我走的是正确的道路吗?任何指针都是有用的,xslt指南很难找到,我在这里试着自学……

EN

回答 3

Stack Overflow用户

发布于 2010-10-29 21:05:38

此转换

代码语言:javascript
复制
<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文档上应用时的

代码语言:javascript
复制
<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>

生成所需的、正确的结果

代码语言:javascript
复制
<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 solution.

  • Keys用于快速searching.

  • All节点按原样复制通过使用identity rule.

  • Templates匹配需要处理的节点覆盖identity rule.

  • Whenever没有找到转换将保留原始文本。
票数 2
EN

Stack Overflow用户

发布于 2010-10-29 21:26:26

此样式表:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pLang" select="'ES'"/>
    <xsl:key name="kElementByLang"
             match="Localization/Content/*[not(self::Nested)]|
                    Localization/Content/Nested/NestedItem"
             use="concat(ancestor::Localization/Locale,'++',
                         name(),'++',Id)"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Localizations"/>
    <xsl:template match="Page/Content//*">
        <xsl:variable name="vMatch"
                      select="key('kElementByLang',
                                  concat($pLang,'++',
                                         name(),'++',
                                         Id))"/>
        <xsl:apply-templates select="$vMatch"/>
        <xsl:if test="not($vMatch)">
            <xsl:call-template name="identity"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出:

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2010-10-29 21:01:28

下面是一个XSLT2.0样式表(您可以使用Saxon 9AltovaXML ToolsXQSharp运行)

代码语言:javascript
复制
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:param name="locale" select="'ES'"/>

  <xsl:key name="k1" 
    match="Localizations/Localization[Locale = $locale]//*[Id]" 
    use="Id"/>
  <xsl:key name="k2" 
    match="Localizations/Localization[Locale = $locale]//*[not(Id) and not(*)]" 
    use="local-name()"/>

  <xsl:template match="Page/Content//*[not(Id) and *]">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Page">
    <xsl:copy>
      <xsl:copy-of select="Id"/>
      <xsl:apply-templates select="Content"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Page//*[Id]">
    <xsl:variable name="loc" select="key('k1', Id)"/>
    <xsl:choose>
      <xsl:when test="$loc">
        <xsl:copy-of select="$loc"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="Page//*[not(Id) and not(*)]">
    <xsl:variable name="loc" select="key('k2', local-name())"/>
    <xsl:choose>
      <xsl:when test="$loc">
        <xsl:copy-of select="$loc"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

但我不确定该样式表是否清楚地陈述和实现了您的需求。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4050128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档