xsl:apply-templates问题,目标文件为空 [英] Problem with xsl:apply-templates, target file empty

查看:70
本文介绍了xsl:apply-templates问题,目标文件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XSLT和C#类XslCompiledTransform将SVG文件转换为另一个XML文件

这是我的SVG

I''m trying to transform a SVG-file into another XML-file using XSLT and the C# class XslCompiledTransform

This is my SVG

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
 
<svg xmlns="http://www.w3.org/2000/svg"

     xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events"

     version="1.1" baseProfile="full"

     width="800mm" height="600mm">

<rect x="10" y="10" width="90" height="40" />
 
</svg>



这是我的C#代码



This is my C#-Code

static void Main(string[] args)
        {
            string xmlSource = args[0];
            string xmlOutput = args[1];
            string xsltFile = args[2];

            // if xslt or soure contains a DTD:
            XmlReaderSettings xmlReadSet = new XmlReaderSettings();
            xmlReadSet.DtdProcessing = DtdProcessing.Parse;
            xmlReadSet.ValidationType = ValidationType.Schema;
            XslCompiledTransform xslt = new XslCompiledTransform();

            // 
            XmlWriterSettings xmlWriteSet = new XmlWriterSettings();
            xmlWriteSet.ConformanceLevel = ConformanceLevel.Auto;

            //
            xslt.Load(xsltFile);
            xslt.Transform(XmlReader.Create(xmlSource, xmlReadSet), XmlWriter.Create(xmlOutput, xmlWriteSet));

        }



这是我的XSLT文件:



And this is my XSLT-File:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"

>
    <xsl:output method="xml" version="1.0" indent="yes" encoding="utf-8" omit-xml-declaration="no"/>

    <xsl:template match="/">
      <xsl:element name="Redlining">
        <xsl:attribute name="hash">???</xsl:attribute>
        <xsl:attribute name="version">4.1</xsl:attribute>

        <xsl:element name="BoundingBox">
          <xsl:attribute name="xMin">???</xsl:attribute>
          <xsl:attribute name="yMin">???</xsl:attribute>
          <xsl:attribute name="xMax">???</xsl:attribute>
          <xsl:attribute name="yMax">???</xsl:attribute>
        </xsl:element>

        <xsl:element name="GeometryHolder" />
        <xsl:apply-templates select="rect"/>
      </xsl:element>
    </xsl:template>

  <xsl:template match="rect">
    <xsl:element name="RectangleHolder">
      <xsl:element name="Sector">
        <xsl:attribute name="type">sector</xsl:attribute>
        <xsl:element name="Coordinate">
          <xsl:attribute name="x">
            <xsl:value-of select="@x + @width"/>
          </xsl:attribute>          
        </xsl:element>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>



这就是我得到的:



And this is what I get:

<Redlining hash="???" version="4.1"><BoundingBox xMin="???" yMin="???" xMax="???" yMax="???" /><GeometryHolder /></Redlining>



我不明白为什么省略xml声明,为什么不缩进xml,最重要的部分是我在定义和调用rect元素的模板时做错了什么.

有人可以帮我吗?



I don''t understand why the xml declaration is omitted, why the xml is not indented and, the most important part, what I''m doing wrong with the definition and calling of the template for the rect elements.

Can anyone help me please?

推荐答案

尝试:
...
<xsl:apply-templates select=".//rect"/>
...




如果这样做没有帮助,则可以尝试以下操作:




If this does not help, you may try the following:

   ...
   <xsl:apply-templates select=".//*[local-name()='rect']"/>
   ...

...
<xsl:template match="*[local-name()='rect']">
...



也许,还有一种更优雅的方法可以忽略元素名称空间.

我在VS2010中运行此程序时遇到了一些问题.如果删除了DOCTYPE部分,并且删除了命名空间,则第一个版本可以立即正常运行.使用原始的svg文件内容,VS2010只需10到15秒钟即可运行该文件(并且仅第二个变体有效).
[/EDIT]

干杯
Andi



Maybe, there is a more elegant way, though, to ignore the element name spaces.

I had some issues while running this in VS2010. If I remove the DOCTYPE part and if I take away the namespaces, the first version works fine and instantly. With the original svg file content, VS2010 takes 10-15 seconds to run that (and only the second variant works).
[/EDIT]

Cheers
Andi


根据我在w3schools.com上得到的答案,我不得不在XSLT中包含svg命名空间,并添加"svg"以排除结果前缀:
According to an answer I got at w3schools.com I had to include the svg namespace in the XSLT and add "svg" to exclude-result-prefixes:

...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:msxsl="urn:schemas-microsoft-com:xslt"

    xmlns:svg="http://www.w3.org/2000/svg"

    exclude-result-prefixes="msxsl svg"

>
...



然后,我可以像这样处理模板:



Then I can address the templates like this:

...
<xsl:apply-templates select="//svg:rect"/>
...

...
<xsl:template match="svg:rect">
...


这篇关于xsl:apply-templates问题,目标文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆