gpt4 book ai didi

xml - XSL在for-each中按属性查找元素

转载 作者:行者123 更新时间:2023-12-03 16:04:10 24 4
gpt4 key购买 nike

在我的XML文档中,我具有以下类型的节点:

<parent>
<value id="value1" name="Y" type="number"/>
<value id="value2" name="X" type="number"/>
<value id="value3" name="Z" type="operation" op="-" args="value1;value2"/>
</parent>


我想将其转换为完整的操作,如下所示:

<parent>
<value id="value1" name="Y" type="number" />
<value id="value2" name="X" type="number" />
<operation>
<name>Z = Y - X</name>
</operation>
</parent>


我正在为我的xsl模板苦苦挣扎。这是完整的XSL代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings">

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

<xsl:template match="value[@type='operation']" priority="1">
<xsl:variable name="name">
<xsl:value-of select="concat(@name, ' = ')" />
<xsl:for-each select="str:tokenize(@args, ';')">
<xsl:choose>
<xsl:when test="//value[@id=current()]">
<xsl:value-of
select="concat(//value[@id=current()]/@name, ' ', @op, ' ')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(current(), ' ', @op, ' ')" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>

<operation>
<name>
<xsl:value-of select="$name" />
</name>
</operation>
</xsl:template>

</xsl:stylesheet>


在每个前台中,我正在检查是否可以找到相应的节点,因为有时操作的@args可能类似于 args="2.00;value1"

for-each内部的测试显然有问题,因为上面显示的输入文件得到的结果是

<?xml version="1.0" encoding="UTF-8"?>
<parent>
<value id="value1" name="Y" type="number" />
<value id="value2" name="X" type="number" />
<operation xmlns:str="http://exslt.org/strings">
<name>Z = value1 value2 </name>
</operation>
</parent>


为了获得正确值的名称,测试应该是什么?

最佳答案

通过XSLT 1和EXSLT str:tokenize,您可以使用

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
exclude-result-prefixes="str"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings">

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

<xsl:key name="val-ref" match="value[@id]" use="@id"/>

<xsl:template match="value[@type='operation']" priority="1">
<xsl:variable name="op" select="."/>
<xsl:variable name="name">
<xsl:value-of select="concat(@name, ' = ')" />
<xsl:for-each select="str:tokenize(@args, ';')">
<xsl:if test="position() > 1">
<xsl:value-of select="concat(' ', $op/@op, ' ')"/>
</xsl:if>
<xsl:variable name="value" select="."/>
<xsl:for-each select="$op">
<xsl:choose>
<xsl:when test="key('val-ref', $value)">
<xsl:value-of
select="key('val-ref', $value)/@name" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>

<operation>
<name>
<xsl:value-of select="$name" />
</name>
</operation>
</xsl:template>

</xsl:stylesheet>


http://xsltransform.net/3MP2uCm

借助XSLT 3,它变得更加紧凑:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">

<xsl:key name="val-ref" match="value[@id]" use="@id"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="value[@op]">
<operation>
<name>
<xsl:value-of select="@name || ' = '"/>
<xsl:value-of select="tokenize(@args, ';') ! (key('val-ref', ., current()/ancestor::parent)/@name, .)[1]"
separator=" {@op} "/>
</name>
</operation>
</xsl:template>

</xsl:stylesheet>


https://xsltfiddle.liberty-development.net/jz1PuP6

关于xml - XSL在for-each中按属性查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58427615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com