Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 我如何编写一个xsl样式表来根据匹配标记的内容翻译文档的某些部分?_Xml_Xslt - Fatal编程技术网

Xml 我如何编写一个xsl样式表来根据匹配标记的内容翻译文档的某些部分?

Xml 我如何编写一个xsl样式表来根据匹配标记的内容翻译文档的某些部分?,xml,xslt,Xml,Xslt,我有一个xml结构,看起来有点像这样: <Page> <Id>Page1</Id> <Content> <Header>This is the Header</Header> <Body>This is the body</Body> <Nested> <NestedItem> <Id>N1</Id> <Con

我有一个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>

第1页
这是标题
这是尸体
N1
这是一个嵌套元素
氮气
这也是一个嵌套元素
锿
卡巴洛酒店
Esta机构
氮气
埃斯塔乌纳元素内斯塔多
在xslt转换之后,我将变量“ES”传递到其中,在本例中,我希望它看起来像这样:

<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>

第1页
卡巴洛酒店
Esta机构
N1
这是一个嵌套元素
氮气
埃斯塔乌纳元素内斯塔多
也就是说,我想翻译本地化中具有相应元素的元素,但保留原始文本中存在原始文本的地方。问题是content元素的结构可能未知,因此我不能在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指南很难找到,我正在这里自学……

这是一个XSLT2.0样式表(您可以使用或运行)



但我不确定该样式表是否清楚地说明并实现了您的需求。

此转换:

<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>
<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>

应用于提供的XML文档时

<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>
<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>

第1页
这是标题
这是尸体
N1
这是一个嵌套元素
氮气
这也是一个嵌套元素
锿
卡巴洛酒店
Esta机构
氮气
埃斯塔乌纳元素内斯塔多
生成所需的正确结果

<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>
<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>

第1页
卡巴洛酒店
Esta机构
N1
这是一个嵌套元素
氮气
埃斯塔乌纳元素内斯塔多
注意事项

<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>
<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>
  • 这是一个纯XSLT1.0解决方案

  • 按键用于快速搜索

  • 使用标识规则“按原样”复制所有节点

  • 与需要处理的节点匹配的模板将覆盖标识规则

  • 如果没有找到翻译,则保留原文

  • 此样式表:

    <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>
    
    
    
    输出:

    <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页
    卡巴洛酒店
    Esta机构
    N1
    这是一个嵌套元素
    氮气
    埃斯塔乌纳元素内斯塔多
    
    问得好,+1。请参阅我的答案,以获得简短、完整和高效的解决方案。:)很好的解决方案(+1),不过有一个bug。变量
    vTranslation
    应为
    。目前,Id与翻译文本连接(结果中额外的“N2”
    N2Esta una element nestado
    )@jasso:感谢您的观察。现在修好了。