如何编写将映射的 xsl

发布于 2025-01-05 07:31:52 字数 7937 浏览 3 评论 0原文

我正在使用两个不同的 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 映射器(请注意具有非空 id 标记)编辑xml文件时生成:

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

I am using two different xslt processors.
One of them creates an element which is not empty in the target (even that this element is empty in the source).

To get to the point, how can I tell the processor
via my xsl that the element should be created in target is empty? (in other words, how to map to that element).

EDITED:
To clearify my problem -
I am using "Altova MapForce" mapping tool and I'm mapping node to node.
The resulted xml (Using the altova xslt processor) is without nodes that are not empty.
The resulted xml (Using the biztalk xslt processor, with the generated xsl from the Altova Mapforce) is with nodes that are not empty.
That nodes are empty on the source.

My Goal is to connect the source node (Using Altova MapForce mapping tool) to a custom written XSLT, and then to connect it to the target.

Here is my code:
in.xml - Instance input for executing the map (Please notice the empty id tag: )

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

Schema1.xsd - source and target schema

<?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 mapping file

<?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 - Xsl generated by the Altova Mapper

    <?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 - Output instance of the BizTalk mapper after using the "Xsl.xsl" for the map (Please notice the non-empty id tag that has been generated when editing the xml file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衬遮猫 2025-01-12 07:31:52

这可能是因为您的输入 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>

That may be because your input XML element has a space in it .. Usage of strip-space should get ur job done!

This rips-off the space between nodes:

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

Input XML:

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

Output XML:

<a>
  <b/>
</a>

This preserves space between nodes :

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

If you want to apply same rule for all elements then

<?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="*" />

source XML:

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

Output XML:

<a>
  <b> </b>
<a>
吃不饱 2025-01-12 07:31:52

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

<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) 处理器包含两个独立的 元素

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

The following transformation demonstrates two different ways of creating empty elements:

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

The result with 7 different XSLT (MSXML3. MSXML4, MSXML6, .NET XslCompiledTransform, .NET XslTransform, Saxon 6.5.4 and AltovaXML (XML-SPY) processors contains two separate <b/> elements:

<t>
   <a>
      <b/>
   </a>
   <a>
      <b/>
   </a>
</t>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文