符合XSL特定属性

时间:2010-11-25 18:37:55

标签: xml xslt

我有一堆docbook文件,它们的imagedata上有不同的属性。我希望他们都拥有1个唯一属性和3个相同的属性:

  <section xmlns="http://docbook.org/ns/docbook" version="5" xml:id="cancelDia">
    <title>Screenshot</title>
    <mediaobject>
      <imageobject>
        <imagedata fileref="screenshots/cancelDialog.png" scalefit="1" width="100%" contentdepth="100%"/>
      </imageobject>
    </mediaobject>
  </section>

fileref是唯一的,应该保持不变,但scalefit,width和contentdepth需要在所有<imagedata>中相同。一个问题是大多数图像数据具有scalefit,少数具有宽度,而稀有图像数据具有contentdepth。即使他们已经具有该属性,我如何确保我的所有<imagedata>具有相同的scalefit,width和contentdepth?

注意:我不确定这是否重要,但我正在使用docbook 5

2 个答案:

答案 0 :(得分:4)

此样式表:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:doc="http://docbook.org/ns/docbook"
 xmlns="http://docbook.org/ns/docbook"
 exclude-result-prefixes="doc">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="doc:imagedata">
        <imagedata fileref="{@fileref}"
                       scalefit="1" width="100%" contentdepth="100%"/>
    </xsl:template>
</xsl:stylesheet>

输出:

<section version="5" xml:id="cancelDia" xmlns="http://docbook.org/ns/docbook">
    <title>Screenshot</title>
    <mediaobject>
        <imageobject>
            <imagedata fileref="screenshots/cancelDialog.png" 
                       scalefit="1" width="100%" contentdepth="100%" />
        </imageobject>
    </mediaobject>
</section>

修改:匹配新的输入样本。

答案 1 :(得分:1)

此转化

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:d="http://docbook.org/ns/docbook"
 xmlns="http://docbook.org/ns/docbook"
 xmlns:ext="http://exslt.org/common"
 exclude-result-prefixes="d ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pAttribs">
  <p scalefit="1" width="100%" contentdepth="100%"/>
 </xsl:param>

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

 <xsl:template match="d:imagedata">
  <xsl:copy>
   <xsl:copy-of select="@fileref
    |
     ext:node-set($pAttribs)/*/@*"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<section xmlns="http://docbook.org/ns/docbook"
         version="5" xml:id="cancelDia">
    <title>Screenshot</title>
    <mediaobject>
        <imageobject>
            <imagedata fileref="screenshots/cancelDialog.png"
             scalefit="1"
             width="100%"
             contentdepth="100%"/>
        </imageobject>
    </mediaobject>
</section>

生成想要的正确结果

<section xmlns="http://docbook.org/ns/docbook"
         version="5" xml:id="cancelDia">
   <title>Screenshot</title>
   <mediaobject>
      <imageobject>
         <imagedata fileref="screenshots/cancelDialog.png" 
                    scalefit="1" width="100%" contentdepth="100%"/>
      </imageobject>
   </mediaobject>
</section>

请注意

所有需要的值都设置为外部<xsl:param>

内元素的属性