【问题标题】:xsl: template and namespacesxsl:模板和命名空间
【发布时间】:2015-05-29 00:06:18
【问题描述】:

我很抱歉再次提出这个问题。我知道这里已经回答了好几次了,但我似乎还是不明白。

我的 XML(基本上是 Open Office draw 的 XML 输出,这里是简化的)

<?xml version="1.0" encoding="UTF-8"?>
<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0" xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2" office:mimetype="application/vnd.oasis.opendocument.graphics">
    <office:body>
        <office:drawing>
            <draw:page draw:name="page1"  draw:master-page-name="Standard">
                <draw:connector    draw:type="curve"     draw:start-shape="id1"  draw:end-shape="id2"   />
                <draw:connector    draw:type="curve"     draw:start-shape="id2"  draw:end-shape="id3"   />
                <draw:connector    draw:type="curve"     draw:start-shape="id3"  draw:end-shape="id4"   />
                <draw:connector    draw:type="curve"     draw:start-shape="id1"  draw:end-shape="id4"   />
                <draw:connector    draw:type="curve"     draw:start-shape="id1"  draw:end-shape="id3"   />
                <draw:custom-shape       >
                    <text:p >
                        <text:span >AAAA,</text:span>
                    </text:p>
                </draw:custom-shape>
                <draw:custom-shape    draw:id="id1"     >
                    <text:p >
                        <text:span >BBBBB</text:span>
                    </text:p>
                </draw:custom-shape>
                <draw:custom-shape    draw:id="id2"     >
                    <text:p >
                        <text:span >CCCCC.</text:span>
                    </text:p>
                </draw:custom-shape>
                <draw:custom-shape    draw:id="id3"     >
                    <text:p >
                        <text:span >DDDDDDDDDD</text:span>
                    </text:p>
                </draw:custom-shape>
                <draw:custom-shape    draw:id="id4"     >
                    <text:p >
                        <text:span >EEEEEEEEEE</text:span>
                    </text:p>
                </draw:custom-shape>
            </draw:page>
        </office:drawing>
    </office:body>
</office:document>

现在我尝试使用

将其转换(为 yEd 转换为 graphml)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
    xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" 
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" 
    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
    xmlns:math="http://www.w3.org/1998/Math/MathML" 
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" 
    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" 
    xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" 
    xmlns:ooo="http://openoffice.org/2004/office" 
    xmlns:ooow="http://openoffice.org/2004/writer" 
    xmlns:oooc="http://openoffice.org/2004/calc" 
    xmlns:dom="http://www.w3.org/2001/xml-events" 
    xmlns:xforms="http://www.w3.org/2002/xforms" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0" 
    xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0" 
    xmlns:rpt="http://openoffice.org/2005/report" 
    xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" 
    xmlns:xhtml="http://www.w3.org/1999/xhtml" 
    xmlns:grddl="http://www.w3.org/2003/g/data-view#" 
    xmlns:officeooo="http://openoffice.org/2009/office" 
    xmlns:tableooo="http://openoffice.org/2009/table" 
    xmlns:drawooo="http://openoffice.org/2010/draw" 
    xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" 
    xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" 
    xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" 
    xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" 
    xmlns:css3t="http://www.w3.org/TR/css3-text/">
    <xsl:template match="/">                        <!-- graphml xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns      http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd" xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://www.yworks.com/xml/yfiles-common/1.0/java" xmlns:sys="http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0" xmlns:x="http://www.yworks.com/xml/yfiles-common/markup/2.0" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" -->
        <graphml xmlns:x="http://www.yworks.com/xml/yfiles-common/markup/2.0" xmlns:yed="http://www.yworks.com/xml/yed/3" xmlns:java="http://www.yworks.com/xml/yfiles-common/1.0/java" xmlns:h="http://graphml.graphdrawing.org/xmlns" xmlns:sys="http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns      http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd" exclude-result-prefixes="h">

            <key for="node" id="d6" yfiles.type="nodegraphics"/>
            <key for="port" id="d2" yfiles.type="portuserdata"/>
            <key attr.name="N_In" attr.type="int" for="node" id="d3">
                <default>0</default>
            </key>
            <graph id="G" edgedefault="directed">
            ====================================
                <xsl:for-each select="//draw:custom-shape|//draw:frame">
                +++++++++++++++++++++++++++++++++++++++++
                    <xsl:element name="node">
                        <xsl:attribute name="id">
                            <xsl:value-of select="@draw:id"/>
                        </xsl:attribute>
                        <xsl:variable name="my_node" select="@draw:id"/><!-- xsl:value-of select="$my_node"/ -->
                        <xsl:element name="data">
                            <xsl:attribute name="key">d4</xsl:attribute>
                            <xsl:value-of select="count(//draw:connector[@draw:start-shape=$my_node])"/>
                        </xsl:element>
                        <xsl:element name="data">
                            <xsl:attribute name="key">d3</xsl:attribute>
                            <xsl:value-of select="count(//draw:connector[@draw:end-shape=$my_node])"/>
                        </xsl:element>
                        <xsl:element name="data">
                            <xsl:attribute name="key">d5</xsl:attribute>
                            <xsl:value-of select="count(//draw:connector[@draw:start-shape=$my_node])-count(//draw:connector[@draw:end-shape=$my_node])"/>
                        </xsl:element>
                        <xsl:apply-templates select="h:In_Counts"/>
                        <xsl:call-template name="h:In_Counts" />
                    </xsl:element>
                </xsl:for-each>
            </graph>
        </graphml>
    </xsl:template>

    <xsl:template match="h:In_Counts" >
            in in_counts ------------------------------------------
    </xsl:template>
</xsl:stylesheet>

我的问题: 我仍然没有得到匹配的(子)模板。 我的思绪在哪里?

提前致谢!

【问题讨论】:

    标签: xslt namespaces


    【解决方案1】:

    有几个问题:

    • 未定义“h”命名空间。
    • &lt;xsl:apply-templates select="h:In_Counts"/&gt; 不匹配,因为 XML 文件中没有 &lt;In_Counts&gt; 元素。
    • &lt;xsl:call-template name="h:In_Counts"/&gt; 失败,因为没有 &lt;xsl:template name="h:In_Counts"&gt;(注意 name 属性)。

    【讨论】:

    • 我确认。 select 属性实际上是从 XML 文件中选择内容,而 name 属性指的是在 XSL 文件中定义的名称。
    • 谢谢你,这个成功了。
      只是为了我的理解和那些处于相同情况的人:我可以将命名空间问题总结如下:
      * &lt;xsl:apply-template select=总是有效针对输入文件并需要其中定义的命名空间。
      * &lt;xsl:call-template name= 针对 xsl 文件工作并且没有命名空间。

      这是正确的吗?
    猜你喜欢
    • 1970-01-01
    • 2015-08-30
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多