【问题标题】:XSL Position doesn't count correctlyXSL 位置计数不正确
【发布时间】:2021-11-25 20:09:30
【问题描述】:

我已经尝试了几天来解决这个 XML 转换问题。 我需要通过 XSLT 文件将带有序号的“标识符”属性添加到 XML 标记中。 我需要指定 XSLT 版本是 1.0。 附言该代码以前在 Internet Explorer 上工作,但转换是通过 JavaScript 进行的,由于需要更新的浏览器支持,我现在无法执行此操作。对于任何人来说,这是一段非常古老的代码,客户不想更改。 我也尝试通过外部文件调用脚本,但也没有用。

这就是 XSL:

<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > 
  <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:attribute name="identifier">
        <xsl:value-of select = "position()" />
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet> 

你有 XML 代替:

<?xml version="1.0" ?>
<root>
  <topic codice="02220020070748129" coobbligati="0" coobbligatiInfo="Unico Intestatario" status="0" statusWorkInProgress="0" imp1="22285" imp2="0" messaggio="" code="0">
    <fDi>-1</fDi>
    <art>
      <comp message="" code="0" importo="22285" numero="14" flag="0" messageEcc="" codeEcc="0"/>
      <def message="" code="0" importo="0" numero="0" flag="0" messageEcc="" codeEcc="0"/>
      <sel message="" code="0" importo="0" numero="0" flag="0" messageEcc="" codeEcc="0"/>
    </art>
    <datascad>0</datascad>
    <orig>0</orig>
    <sosp>0</sosp>
  </topic>
</root>

现在,在示例中使用 XSLT,结果肯定是错误的,还计算了属性和标签关闭的数字。 下面是一个例子:

<?xml version="1.0"?>
<root identifier="1">
  <topic identifier="2" codice="02220020070748129" coobbligati="0" coobbligatiInfo="Unico Intestatario" status="0" statusWorkInProgress="0" imp1="22285" imp2="0" messaggio="" code="0">
    <fDi identifier="11">-1</fDi>
    <art identifier="13">
      <comp identifier="2" message="" code="0" importo="22285" numero="14" flag="0" messageEcc="" codeEcc="0"/>
      <def identifier="4" message="" code="0" importo="0" numero="0" flag="0" messageEcc="" codeEcc="0"/>
      <sel identifier="6" message="" code="0" importo="0" numero="0" flag="0" messageEcc="" codeEcc="0"/>
    </art>
    <datascad identifier="15">0</datascad>
    <orig identifier="17">0</orig>
    <sosp identifier="19">0</sosp>
  </topic>
</root>

相反,我需要的结果如下:

<?xml version="1.0"?>
  <root identifier="1">
    <topic identifier="2" codice="02220020070748129" coobbligati="0" coobbligatiInfo="Unico Intestatario" status="0" statusWorkInProgress="0" imp1="22285" imp2="0" messaggio="" code="0">
      <fDi identifier="3">-1</fDi>
      <art identifier="4">
      <comp identifier="5" message="" code="0" importo="22285" numero="14" flag="0" messageEcc="" codeEcc="0"/>
      <def identifier="6" message="" code="0" importo="0" numero="0" flag="0" messageEcc="" codeEcc="0"/>
      <sel identifier="7" message="" code="0" importo="0" numero="0" flag="0" messageEcc="" codeEcc="0"/>
    </art>
    <datascad identifier="8">0</datascad>
    <orig identifier="9">0</orig>
    <sosp identifier="10">0</sosp>
  </topic>
</root>

提前感谢每一个贡献。

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    你的方法有两个问题:

    1. 您正在为 XML 文档中的每个 节点(元素、属性、文本)分配一个标识符;

    2. position() 函数在当前上下文中工作,而不是在整个 XML 文档中。

    我猜你想这样做:

    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"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="*">
        <xsl:copy>
            <xsl:attribute name="identifier">
                <xsl:number count="*" level="any"/>
            </xsl:attribute>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 您好!它确实有效!非常感谢您的即时回复。我实际上忘了指定,我需要从 0 开始计数,而不是从 1 开始计数。是否有任何可选配置可以放入?提前致谢!
    • 如果您仅限于 XSLT 1.0,则先将数字放入变量中,然后从变量中减去 1。
    • 好的,完成!它适用于以下 XLST:` w3.org/1999/XSL/Transform"> `再次感谢!
    • 关于您的标题问题,position() 的行为完全正确,您只是误解了它的工作原理。如果您首先假设软件是正确的,那么您可能会发现调试会更容易,并且在证明并非如此之前您是错误的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多