【问题标题】:How to create a new entry based on a condition in xslt如何根据 xslt 中的条件创建新条目
【发布时间】:2013-11-07 18:43:19
【问题描述】:

输入 XML:

.....
..... <!-- Many lines of code -->
.....
<Attribute Name="Attr1">  
  <Array>
       <Object Type="Object1">
          <Attribute Name="Attr2">
              "123"
              "234"
           </Attribute>
           <Attribute Name="Attr3">"456"</Attribute>
        </Object>
        <Object Type="Object2">
           <Attribute Name="Attr4">
               "444"
               "555"
            </Attribute>               
         </Object>
         <Object Type="Object3">
            <Attribute Name="Attr5">
                "666"   
                "777"
                "888"     
    <!-- My new item should come here -->   
             </Attribute>
          </Object> 
   </Array>
</Attribute>

我尝试使用以下 XSLT 将新条目 (999) 添加到上述位置“我的新项目应该到这里”。 在插入这个新项目之前,我想检查节点Attribute(&lt;Attribute Name = "Attr5") 的值是否为“888”。只有当它包含 "888" 时,我才应该在之后插入 "999" 。你能告诉我这是如何实现的吗?

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

     <xsl:template match="@* | node()">
      <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
     </xsl:template>

     <xsl:variable name="obj" select='"Object3"'/>
     <xsl:variable name="attr" select='"Attr5"'/>
     <xsl:param name="evalue">"999"</xsl:param>

     <xsl:template match="Attribute/Array/Object[@Type=$obj]/Attribute[@Name=$attr]">
      <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:value-of select="." />
    <xsl:value-of select="$evalue"/>
  </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>

输出 XML

<Attribute Name="Attr1">  
  <Array>
       <Object Type="Object1">
          <Attribute Name="Attr2">
              "123"
              "234"
           </Attribute>
           <Attribute Name="Attr3">"456"</Attribute>
        </Object>
        <Object Type="Object2">
           <Attribute Name="Attr4">
               "444"
               "555"
            </Attribute>               
         </Object>
         <Object Type="Object3">
            <Attribute Name="Attr5">
                "666"   
                "777"
                "888"     
                "999"
             </Attribute>
          </Object> 
   </Array>
</Attribute>

这就是最终 XML 的样子。请帮帮我

【问题讨论】:

  • 您可以使用 XSLT 2.0 还是只使用 XSLT 1.0?另外,您能否准确地说出尝试时会发生什么(即错误或意外输出),因为说“这不起作用”有点含糊。谢谢!
  • @TimC 我没有得到预期的输出。我正在使用 XSLT 1.0,但我的环境不支持 XSLT 2.0

标签: xml xslt


【解决方案1】:

您的模板中缺少许多说明...

<xsl:template match="Attribute/Array/Object[@Type=$obj]/Attribute[@Name=$attr and contains(., '&quot;888&quot;')]">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:value-of select="." />
    <xsl:value-of select="$evalue"/>
  </xsl:copy>
</xsl:template>

【讨论】:

  • 感谢这项工作,我还有一个问题.. 在插入这个新项目之前,我想检查节点 Attribute(
  • 我已根据您的新要求更新了答案。
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多