【问题标题】:How to write xsl that will map <xsl:text></xsl:text>如何编写将映射 <xsl:text></xsl:text> 的 xsl
【发布时间】:2012-03-05 17:21:31
【问题描述】:

我正在使用两个不同的 xslt 处理器。 其中一个创建了一个在目标中不为空的元素(即使该元素在源中为空)。

直截了当,我该如何告诉处理器 通过我的 xsl 应该在目标中创建的元素是空的? (换句话说,如何映射到那个元素)。

已编辑: 为了澄清我的问题 - 我正在使用“Altova MapForce”映射工具,并且正在将节点映射到节点。 结果 xml(使用 altova xslt 处理器)没有非空节点。 生成的 xml(使用 biztalk xslt 处理器,以及从 Altova Mapforce 生成的 xsl)包含非空节点。 源上的节点是空的。

我的目标是将源节点(使用 Altova MapForce 映射工具)连接到自定义编写的 XSLT,然后将其连接到目标。

这是我的代码: in.xml - 执行地图的实例输入(请注意空的 id 标签:)

<ns0:Root xmlns:ns0="http://BizTalk_Server_Project1.Schema1">
  <id root="root_0" extension="extension_1" />
</ns0:Root>

Schema1.xsd - 源架构和目标架构

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://BizTalk_Server_Project1.Schema1" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://BizTalk_Server_Project1.Schema1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id">
          <xs:complexType>
            <xs:attribute name="root" type="xs:string" />
            <xs:attribute name="extension" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

New.mfd - Altova 映射文件

<?xml version="1.0" encoding="UTF-8"?>
<mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="19">
    <component name="defaultmap1" blackbox="0" uid="1" editable="1">
        <properties SelectedLanguage="xslt"/>
        <structure>
            <children>
                <component name="document" library="xml" uid="4" kind="14">
                    <properties/>
                    <view rbx="150" rby="200"/>
                    <data>
                        <root>
                            <header>
                                <namespaces>
                                    <namespace/>
                                    <namespace uid="http://BizTalk_Server_Project1.Schema1"/>
                                    <namespace uid="http://www.altova.com/mapforce"/>
                                </namespaces>
                            </header>
                            <entry name="FileInstance" ns="2" expanded="1">
                                <entry name="document" ns="2" expanded="1" casttotargettypemode="cast-in-subtree">
                                    <entry name="Root" ns="1" expanded="1">
                                        <entry name="id" expanded="1">
                                            <entry name="extension" type="attribute" outkey="4"/>
                                        </entry>
                                    </entry>
                                </entry>
                            </entry>
                        </root>
                        <document schema="Schema1.xsd" outputinstance="Schema1.xml" instanceroot="{http://BizTalk_Server_Project1.Schema1}Root"/>
                        <wsdl/>
                    </data>
                </component>
                <component name="document" library="xml" uid="5" kind="14">
                    <properties XSLTDefaultOutput="1"/>
                    <view ltx="593" rbx="743" rby="200"/>
                    <data>
                        <root>
                            <header>
                                <namespaces>
                                    <namespace/>
                                    <namespace uid="http://BizTalk_Server_Project1.Schema1"/>
                                    <namespace uid="http://www.altova.com/mapforce"/>
                                </namespaces>
                            </header>
                            <entry name="FileInstance" ns="2" expanded="1">
                                <entry name="document" ns="2" expanded="1" casttotargettypemode="cast-in-subtree">
                                    <entry name="Root" ns="1" expanded="1">
                                        <entry name="id" expanded="1">
                                            <entry name="extension" type="attribute" inpkey="5"/>
                                        </entry>
                                    </entry>
                                </entry>
                            </entry>
                        </root>
                        <document schema="Schema1.xsd" outputinstance="Schema1.xml" instanceroot="{http://BizTalk_Server_Project1.Schema1}Root"/>
                        <wsdl/>
                    </data>
                </component>
            </children>
            <graph directed="1">
                <edges/>
                <vertices>
                    <vertex vertexkey="4">
                        <edges>
                            <edge vertexkey="5" edgekey="6"/>
                        </edges>
                    </vertex>
                </vertices>
            </graph>
        </structure>
    </component>
</mapping>

Xsl.xsl - Altova Mapper 生成的 Xsl

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://BizTalk_Server_Project1.Schema1" xmlns:agt="http://www.altova.com/Mapforce/agt" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="ns0 agt xs">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template name="agt:var2_MapToSchema1_function">
        <xsl:param name="par0"/>
        <xsl:attribute name="extension">
            <xsl:value-of select="string($par0/@extension)"/>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="/">
        <Root xmlns="http://BizTalk_Server_Project1.Schema1">
            <xsl:attribute name="xsi:schemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance">http://BizTalk_Server_Project1.Schema1 C:/Users/OhadAv/Desktop/ForAltova/Schema1.xsd</xsl:attribute>
            <id xmlns="">
                <xsl:for-each select="ns0:Root/id">
                    <xsl:variable name="var1_extension">
                        <xsl:if test="@extension">
                            <xsl:value-of select="'1'"/>
                        </xsl:if>
                    </xsl:variable>
                    <xsl:if test="string(boolean(string($var1_extension))) != 'false'">
                        <xsl:call-template name="agt:var2_MapToSchema1_function">
                            <xsl:with-param name="par0" select="."/>
                        </xsl:call-template>
                    </xsl:if>
                </xsl:for-each>
            </id>
        </Root>
    </xsl:template>
</xsl:stylesheet>

GeneratedByBizTalkMapperAfterXSLTmap.xml - 为地图使用“Xsl.xsl”后的 BizTalk 映射器的输出实例(请注意编辑 xml 文件时生成的非空 id 标记:

<?xml version="1.0" encoding="utf-8"?>
<Root xsi:schemaLocation="http://BizTalk_Server_Project1.Schema1 C:/Users/OhadAv/Desktop/ForAltova/Schema1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://BizTalk_Server_Project1.Schema1">
  <id extension="extension_1" xmlns="">
  </id>
</Root>

【问题讨论】:

  • 你还没有完成你的“功课”:没有源 XML,没有 XSLT 代码(请完整且尽可能简单和小),没有想要的结果,也没有实际的结果......拜托,编辑并将这个猜测练习变成一个真正的问题。
  • @DimitreNovaatchev,我同意你的看法..
  • 您好。谢谢你的帮助。我已将我的问题扩展到您的要求。

标签: xml xslt biztalk xslt-1.0


【解决方案1】:

这可能是因为您的输入 XML 元素中有一个空格 .. 使用 strip-space 应该可以完成您的工作!

这会破坏节点之间的空间:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="YourElement" />
<!--Your code here-->

输入 XML:

<a>
  <b> </b>
<a>

输出 XML:

<a>
  <b/>
</a>

这样可以保留节点之间的空间:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="YourElement" />
<!--Your code here-->

如果你想对所有元素应用相同的规则,那么

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="*" />

源 XML:

<a>
  <b> </b>
</a>

输出 XML:

<a>
  <b> </b>
<a>

【讨论】:

  • 标头声明与此无关:)
  • @Haim,请提供相应的示例 XML 和所需的输出,我根据提问者的句子得出了这个答案。我不确定你的情况是什么。跨度>
【解决方案2】:

以下转换演示了创建空元素的两种不同方式

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <t>
       <a>
         <b/>
       </a>
       <a>
         <xsl:element name="b"/>
       </a>
     </t>
 </xsl:template>
</xsl:stylesheet>

7 种不同 XSLT(MSXML3.MSXML4、MSXML6、.NET XslCompiledTransform、.NET XslTransform、Saxon 6.5.4 和 AltovaXML (XML-SPY) 处理器的结果包含两个单独的 &lt;b/&gt; 元素

<t>
   <a>
      <b/>
   </a>
   <a>
      <b/>
   </a>
</t>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2015-02-07
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多