开发者

XSL Specific Attribute conforming

I have a bunch of docbook files that have have varying attributes on on the开发者_StackOverflowir imagedata. I want to have them all have 1 unique attribute, and 3 identical attributes:

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

The fileref is unique and should be left alone, but scalefit, width, and contentdepth need to be the same among all <imagedata>. One problem is that most image data have scalefit, a few have width, and the rare one has contentdepth. How do I ensure, even if they already have that attribute, all my <imagedata> have identical scalefit, width, and contentdepth?

Note: I'm not sure if this matters, but I'm using docbook 5


This stylesheet:

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

Output:

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

Edit: Matching new input sample.


This transformation:

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

when applied on the provided XML document:

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

produces the wanted, correct result:

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

Do note:

All the desired values are set as attributes of an element inside of an external <xsl:param>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜