将整个 XML 作为属性输出 [英] Output entire XML as an attribute

查看:22
本文介绍了将整个 XML 作为属性输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XML 和 XSL 样式表还很陌生,我的任务是为我们的一个客户创建一个样式表.我已经创建了一个以下列格式输出 XML 的样式表:

I'm fairly new to XML and XSL Stylesheets, and I've been tasked with creating a stylesheet for one of our clients. I have already created a stylesheet that outputs an XML in the following format:

<Trip TripType="Normal">
    <Plan BeginTime="2011-08-13T10:00:00" UserDefinedTripID="777" UserDefinedRouteID="777">
        <PlanStop ArrivalTime="2011-08-13T15:30:00" ArrivalLock="true" SiteID="1" PassThru1="test1" PassThru2="test2" PassThru3="test3" PassThru4="test4">
            <PlanNote Line1="Freeform Text" Line2="Line2" Line3="Line3" />
            <PlanCargo Duration="60" BillID="" Weight="100" Units="100.0" XUnitTypeID="10" Action="Pick" />
            <PlanNote Line1="Freeform Text" Line2="Line2" Line3="Line3" />
            <PlanCargo Duration="60" BillID="" Weight="100" Units="100.0" XUnitTypeID="12" Action="Pick" />
        </PlanStop>
    </Plan>
</Trip>

我需要获取输出并将内容插入到 Trip 元素内的一个属性中,如下所示:

I need to take the output and insert the contents into an attribute within the Trip element to look like this:

<Trip TripID="-1" CurrentRevisionNumber="1" IsDispatch="1" IsActive="0" 
IsComplete="0" OrganizationID="4"
TripData="&lt;Trip TripType=&quot;Normal&quot;&gt;
  &lt;Plan BeginTime=&quot;2011-08-13T10:00:00&quot; UserDefinedTripID=&quot;777&quot;
  UserDefinedRouteID=&quot;777&quot;&gt;
    &lt;PlanStop ArrivalTime=&quot;2011-08-13T10:00:00&quot; ArrivalLock=&    quot;true&quot; SiteID=&quot;1&quot; PassThru1=&quot;test1&quot; PassThru2=&    quot;test2&quot; PassThru3=&quot;test3&quot; PassThru4=&quot;test4&quot;&gt;
    &lt;PlanCargo Duration=&quot;45&quot; BillID=&quot;&quot; Weight=&    quot;100&quot; Units=&quot;100.0&quot; XUnitTypeID=&quot;9&quot; Action=&quot;Pick&quot;     /&gt;
    &lt;/PlanStop&gt; />

所以换句话说,我需要在执行一些字符转换时获取现有的 XML 输出并将其放入一个属性中.

So in other words, I need to take an existing XML output and put it in an attribute while performing some character transformations.

是的,它非常丑陋,但这就是他们想要的.我正在考虑制作另一个 XSL,它将复制来自原始 XSL 转换的 XML 输出并将其放在一个属性中,同时将 <、>、" 等转换为 <、>、" 等(不确定是什么他们被称为).

Yes, it is extremely ugly, but this is how they want it. I was thinking of making another XSL that will copy over the XML output from the original XSL transformation and place it in an attribute while converting <, >, ", etc into < , > , " , etc (not sure what they're called).

我已经在互联网上搜索了解决方案,但我似乎找不到任何类似的解决方案(我想这是因为这是一个荒谬的要求).如有必要,我可以提供我的原始 XSL,但如果可能,我宁愿不更改它.

I've scoured the internet for solutions, but I can't seem to find any that are quite like this (I'd imagine that's because this is a ridiculous request). I can provide my original XSL if necessary, but I'd rather not change it, if possible.

提前致谢!

推荐答案

我会从别人的愚蠢中节省自己的时间、精力和沮丧,并将通过使用微小扩展功能的微小转换来实现这一点.

以下是使用 .NET XslCompiledTransform XSLT 处理器的示例:

Here is example using the .NET XslCompiledTransform XSLT processor:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:my="my:my" exclude-result-prefixes="msxsl my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <t stupid="{my:stringize(.)}"/>
 </xsl:template>

 <msxsl:script language="c#" implements-prefix="my">
  public string stringize(XPathNavigator doc)
  {
   return doc.InnerXml;
  }
 </msxsl:script>
</xsl:stylesheet>

当应用于任何 XML 文档时,它会生成带有 stupid 属性的顶部元素,该属性包含顶部元素主体的转义表示.

When applied on any XML document, it produces the top element with a stupid attribute that contains the escaped representation of the body of the top element.

例如,当转换应用于此 XML 文档时:

<t>
  <a>
    <b x="y">
      <!--  Comment here -->
      <?aPI ?>
    </b>
  </a>
</t>

产生想要的结果:

<t stupid="&#xD;&#xA;  &lt;a&gt;&#xD;&#xA;    &lt;b x=&quot;y&quot;&gt;&#xD;&#xA;      &lt;!--  Comment here --&gt;&#xD;&#xA;      &lt;?aPI?&gt;&#xD;&#xA;    &lt;/b&gt;&#xD;&#xA;  &lt;/a&gt;&#xD;&#xA;" />

最有可能为几乎每个 XSLT 处理器编写实现相同想法的转换,并带有扩展函数,该函数本身可以用各种编程语言编写.

A transformation implementing the same idea most probably can be written for almost every XSLT processor with an extension function that itself can be written in variety of programming languages.

这篇关于将整个 XML 作为属性输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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