【问题标题】:Add CDATA to a Node Value in a loop在循环中将 CDATA 添加到节点值
【发布时间】:2018-12-13 18:31:28
【问题描述】:

** 更新了输出 ** 我正在尝试将 Add CDATA to an xml file 但到目前为止似乎没有一个工作。 我什至尝试按字面意思添加它,但正如预期的那样,它没有成功。 任何人都可以在这方面帮助我。

这是我的节点的生成方式

<xsl:for-each select="$OLifE/">
                    <DataPoint>
                      <Name>Carrier.Requirements<xsl:if test="$NumberOfPayments > 1"><xsl:value-of select="position()"/></xsl:if></Name>
                      <Value>Here is the response text</Value>
</DataPoint>

我的预期输出是

<DataPoint>
    <Name>Carrier.Requirements1</Name>
    <Value><![CDATA[Here is the response text]]</Value>
</DataPoint>
<DataPoint>
    <Name>Carrier.Requirements2</Name>
    <Value><![CDATA[Here is the response text]]</Value>
</DataPoint>
<DataPoint>
    <Name>Carrier.Requirements3</Name>
    <Value><![CDATA[Here is the response text]]</Value>
</DataPoint>

如果需要任何进一步的信息,请告诉我。

【问题讨论】:

  • 请提供minimal reproducible example,包括输入和预期输出。
  • 仍然没有输入。而且您的预期输出没有任何 CDATA - 所以不清楚您的问题是什么。
  • @michael.hor257k:当你说输入时,你是什么意思?我认为输入是来自第一个代码部分中循环的值。
  • XSLT 的输入是一个 XML 文档。

标签: c# xslt cdata


【解决方案1】:

这是一个简化的例子:

XML

<input>
    <item/>
    <item/>
    <item/>
</input>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" cdata-section-elements="Value"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/input">
    <output>
        <xsl:for-each select="item">
            <DataPoint>
                <Name>
                    <xsl:value-of select="concat('Carrier.Requirements', position())"/>
                </Name>
                <Value>Here is the response text</Value>
            </DataPoint>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="utf-16"?>
<output>
  <DataPoint>
    <Name>Carrier.Requirements1</Name>
    <Value><![CDATA[Here is the response text]]></Value>
  </DataPoint>
  <DataPoint>
    <Name>Carrier.Requirements2</Name>
    <Value><![CDATA[Here is the response text]]></Value>
  </DataPoint>
  <DataPoint>
    <Name>Carrier.Requirements3</Name>
    <Value><![CDATA[Here is the response text]]></Value>
  </DataPoint>
</output>

演示https://xsltfiddle.liberty-development.net/94hvTAn

【讨论】:

  • 非常感谢 Michael 的及时帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 2012-08-03
  • 2017-10-07
  • 1970-01-01
相关资源
最近更新 更多