Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
XSLT-使用Xsl将Xml转换为Xml_Xml_Xslt_Import_Xslt 1.0_Xslt 2.0 - Fatal编程技术网

XSLT-使用Xsl将Xml转换为Xml

XSLT-使用Xsl将Xml转换为Xml,xml,xslt,import,xslt-1.0,xslt-2.0,Xml,Xslt,Import,Xslt 1.0,Xslt 2.0,我有两个xml文件,我想使用第一个和第二个xml文件创建一个新的xml文件。我想使用第二个文件节点修改节点 第一个文件: [编辑] 我想要得到的结果是: <placeName id="en.0" ref="file:fr_Cussac_coord_entites.xml"> <name> <w type="NPr">Cussac</w> </name> <

我有两个xml文件,我想使用第一个和第二个xml文件创建一个新的xml文件。我想使用第二个文件节点修改
节点

第一个文件: [编辑]

我想要得到的结果是:

<placeName id="en.0" 
         ref="file:fr_Cussac_coord_entites.xml">
         <name>
          <w type="NPr">Cussac</w>
         </name>
</placeName>

库萨克
到目前为止我尝试了什么,但无法获取id值:

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:variable name="path">file:fr_Cussac-cood-entites.xml</xsl:variable>
    <xsl:key name="ref" match="entite_nom" use="."/>

    <xsl:param name="doc2" select="document('fr_Cussac-coord-entites.xml')/geolocalisation"/>  

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="tei:name[key('ref', tei:w[@type], $doc2)]">
        <placeName id="{key('ref', w[@type], $doc2)/@id}" ref="{$path}">
            <xsl:copy-of select="."/>
        </placeName>   
</xsl:template>
</xsl:stylesheet>

文件:fr_Cussac-cood-entites.xml
我得到的是一个空id,无法获得id值:

<placeName id="" ref="file:fr_Cussac-cood-entites.xml">
            <name>
               <w type="NAM">Cussac</w>
            </name>
</placeName>

库萨克

这似乎是钥匙的工作:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:key name="ref" match="entite_nom" use="."/>

  <xsl:param name="doc2" select="doc('file2.xml')"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="tei:name[key('ref', tei:w[@type], $doc2)]">
     <placeName id="{key('ref', tei:w[@type], $doc2)/@id}">
         <xsl:copy-of select="."/>
     </placeName> 
  </xsl:template>

</xsl:stylesheet>


在XSLT 3中,也可以使用复合键或累加器根据位置进行区分,我目前不确定哪种方法更好,以上主要是尝试根据您更改的、更复杂的要求调整先前的建议。

我尝试了您的答案,但无法获得id值,它是空的,我编辑了我的帖子,所以你可以看到我得到了什么,我得到了什么tried@Sat,很抱歉,我没有将
tei
前缀添加到内键调用参数,现已更正。对于您的示例,它会找到三项,不确定这是否是您想要的。谢谢,这更好,但我正在尝试获取第一个Cussac id=en.0和第二个Cussac id=en.6最后一个id=0.7除Cussac之外的其他值都是正确的。@Sat,您可以编辑输入示例吗?是否有几个
name
w
元素具有相同的值
Cussac
?我添加了输入示例,我的文件中有3个
w
Cussac。
<placeName id="" ref="file:fr_Cussac-cood-entites.xml">
            <name>
               <w type="NAM">Cussac</w>
            </name>
</placeName>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:key name="ref" match="entite_nom" use="."/>

  <xsl:param name="doc2" select="doc('file2.xml')"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="tei:name[key('ref', tei:w[@type], $doc2)]">
     <placeName id="{key('ref', tei:w[@type], $doc2)/@id}">
         <xsl:copy-of select="."/>
     </placeName> 
  </xsl:template>

</xsl:stylesheet>
  <xsl:template match="tei:name[key('ref', tei:w[@type], $doc2)]">
     <xsl:variable name="word" select="tei:w[@type]"/>
     <xsl:variable name="pos" as="xs:integer">
         <xsl:number level="any" count="tei:name[tei:w[@type] = $word]"/>
     </xsl:variable>
     <placeName id="{key('ref', tei:w[@type], $doc2)[$pos]/@id}">
         <xsl:copy-of select="."/>
     </placeName> 
  </xsl:template>