Xml XSL特定属性一致性

Xml XSL特定属性一致性,xml,xslt,Xml,Xslt,我有一堆docbook文件,它们的imagedata上有不同的属性。我希望它们都具有1个唯一属性和3个相同属性: <section xmlns="http://docbook.org/ns/docbook" version="5" xml:id="cancelDia"> <title>Screenshot</title> <mediaobject> <imageobject> <im

我有一堆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必须相同。一个问题是,大多数图像数据都有scalefit,少数有width,少数有contentdepth。我如何确保,即使它们已经具有该属性,我的所有
都具有相同的scalefit、width和contentdepth

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

此样式表:

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

截图
编辑:匹配新输入样本。

此样式表:

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

截图

编辑:匹配新输入样本。

此转换

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

应用于提供的XML文档时

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

应用于提供的XML文档时

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

所有所需的值都设置为外部

@Alejandro:是的,这适用于我的原始问题,尽管我似乎无法使它适用于docbook命名空间。我已经更新了我的问题,因此它更接近我所看到的内容。您可能需要声明docbook名称空间
xmlns:docbook=”http://docbook.org/ns/docbook“
在XSLT中,并调整
@match=“docbook:imagedata”
我已经尝试过了,不幸的是,出于某种原因,它还将:xmlns=”“xmlns:d=”“抛出到正确答案+1中。我提供了自己的名称空间,它只针对默认名称空间进行调整,并使用参数作为值。@Ace:这是因为您需要名称空间URI来执行两项任务:匹配输入
imagedata
和输出
imagedata
元素。但是,因为匹配的XPath模式需要前缀-URI绑定,所以必须在作用域名称空间中为文字结果元素去掉它。@Alejandro:是的,这适用于我的原始问题,尽管我似乎无法使它适用于docbook名称空间。我已经更新了我的问题,因此它更接近我所看到的内容。您可能需要声明docbook名称空间
xmlns:docbook=”http://docbook.org/ns/docbook“
在XSLT中,并调整
@match=“docbook:imagedata”
我已经尝试过了,不幸的是,出于某种原因,它还将:xmlns=”“xmlns:d=”“抛出到正确答案+1中。我提供了自己的名称空间,它只针对默认名称空间进行调整,并使用参数作为值。@Ace:这是因为您需要名称空间URI来执行两项任务:匹配输入
imagedata
和输出
imagedata
元素。但是,因为匹配的XPath模式需要前缀-URI绑定,所以必须在作用域名称空间中为文本结果元素去掉这个绑定。请参阅我的答案,了解与@Alejandro几乎相同的解决方案,该解决方案只调整默认名称空间问题并使用参数化方法好的问题+1。请参阅我的答案,了解与@Alejandro几乎相同的解决方案,该解决方案只调整默认名称空间问题,并使用参数化方法。为什么imagedata标记会更改并在其中添加XMLN?有没有办法防止这种情况发生?编辑似乎您只需要将docbook设置为deault命名空间?回答得也不错。@Ace:观察得不错。不仅可以看到此修复,还可以看到一种新的、更好的参数化技术。:)为什么imagedata标记会更改并在其中添加xmlns?有没有办法防止这种情况发生?编辑似乎您只需要将docbook设置为deault命名空间?回答得也不错。@Ace:观察得不错。不仅可以看到此修复,还可以看到一种新的、更好的参数化技术。:)