xml - 使用 XSLT 1.0 根据 ID 值替换 xml 文件的属性值

标签 xml xslt xpath xslt-1.0

我有一个像这样的主要 XML 文档:

<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter1">
<title>First chapter</title>
      <section xml:id="section1">
                <imageobject>
                    <id>aa12</id>
                    <image fileref="image1.jpg"/>
                </imageobject>
                <imageobject>
                    <id>bb13</id>
                    <image fileref="image2.jpg"/>
                </imageobject>
      </section>
      <section xml:id="section2" xml:base="../other/section1.xml">  
                    <imageobject>
                        <id>ab14</id>
                        <image fileref="image1.jpg"/>
                    </imageobject>
                    <imageobject>
                        <id>ab15</id>
                        <image fileref="image2.jpg"/>
                    </imageobject>

             <section xml:id="section3" xml:base="../some-other/more/section3.xml">
                    <imageobject>
                        <id>ac16</id>
                        <image fileref="image1.jpg"/>
                    </imageobject>
             </section>
    </section>
    <section xml:id="section4" xml:base="../some-other/section4.xml">
                    <imageobject>
                        <id>ac17</id>
                        <image fileref="image2.jpg"/>
                    </imageobject>
    </section>
 </chapter>

以及另一个 XML 文件和值,例如:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <NewData>
       <Rename id="ab14" imageName="aaaa.jpg"/>
       <Rename id="ab15" imageName="bbbb.jpg"/>
       <Rename id="ac16" imageName="cccc.jpg"/>
       <Rename id="ac17" imageName="dddd.jpg"/>
    </NewData>

最后我需要一个类似下面的输出,根据 id 值将其替换为正确的重命名图像名称。

<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter1">
    <title>First chapter</title>
    <section xml:id="section1">
                    <imageobject>
                        <image fileref="image1.jpg"/>
                    </imageobject>
                    <imageobject>
                        <image fileref="image2.jpg"/>
                    </imageobject>
    </section>
    <section xml:id="section2" xml:base="../other/section1.xml">  
                        <imageobject>
                            <image fileref="aaaa.jpg"/>
                        </imageobject>
                        <imageobject>
                            <image fileref="bbbb.jpg"/>
                        </imageobject>

             <section xml:id="section3" xml:base="../some-other/more/section3.xml">
                        <imageobject>
                            <image fileref="cccc.jpg"/>
                        </imageobject>
             </section>
   </section>
   <section xml:id="section4" xml:base="../some-other/section4.xml">
                        <imageobject>
                            <image fileref="dddd.jpg"/>
                        </imageobject>
   </section>
</chapter>

这里发生的情况是这样的:如果第一个 XML 文档中的 id 属性与第二个 XML 文档中的 id 属性匹配,则 fileref 的值第一个文档中的 code> 被第二个 XML 文件中匹配的 imageName 替换。

请参阅我在此处给出的示例。

如何使用 XSLT 1.0 执行此操作?

我正在使用 Saxon 或 Xsltproc 处理器。

提前致谢..!!

最佳答案

这是一个 XSLT 1.0 样式表(使用 xsltproc 和 Saxon 6 都支持的 exsl:node-set):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  exclude-result-prefixes="exsl">

<xsl:param name="doc2-url" select="'test2012080602.xml'"/>
<xsl:variable name="doc2" select="document($doc2-url)"/>

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:key name="img-by-id" match="Rename" use="@id"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="imageobject">
   <xsl:copy>
     <xsl:variable name="id" select="id"/>
     <xsl:variable name="ref">
       <xsl:for-each select="$doc2">
         <xsl:if test="key('img-by-id', $id)">
           <image filref="{key('img-by-id', $id)/@imageName}"/>
         </xsl:if>
       </xsl:for-each>
     </xsl:variable>
     <xsl:variable name="new" select="exsl:node-set($ref)/image"/>
     <xsl:choose>
       <xsl:when test="$new">
         <xsl:copy-of select="$new"/>
       </xsl:when>
       <xsl:otherwise>
         <xsl:copy-of select="image"/>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>

您可以将参数doc2-url设置为具有新名称的辅助输入文档的位置。

关于xml - 使用 XSLT 1.0 根据 ID 值替换 xml 文件的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11823668/

相关文章:

c# - 如何从 C# 中的 ListView 写入 XML 文件

c# - 如何使用 Linq 反序列化 xml?

java - 我需要一个简单的命令行程序来使用 XSL 样式表转换 XML

xpath - Xpath,用于检索父级内部的所有标签

java - org.jdom.xpath.Xpath 不返回 UTF-8

c - Yang 的 XPath current()

java - 如何在 Fragments 中处理 onClick

xml - 在 XPath 中提取以下同级节点的子节点属性

xslt - 使用 XSLT 解析 CData 中的 XML 元素并打印到 HTML

html - Xpath:从<li>中的特定<span>中选择内容