Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT:是否修改变量列表中匹配的属性?_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

Xml XSLT:是否修改变量列表中匹配的属性?

Xml XSLT:是否修改变量列表中匹配的属性?,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我试图编写XSLT代码,仅当开头与变量oldRoot中的一个字符串匹配时,才会修改bind元素的ref属性。此变量将是动态的,并将在生产环境中保存不同数量的字符串。我当前的解决方案只适用于一个字符串,但当我开始将其应用于变量中的多个字符串时,“Others”-子句掩盖了修改,除非匹配的是最后一个字符串。如何分解最后的“xsl:copy of”-语句,以便仅当变量oldRoot中的字符串都不匹配时才应用?我只能访问XSLT1.0 简化要求:我希望XSLT编辑bind元素的ref属性,其中开头匹配“

我试图编写XSLT代码,仅当开头与变量oldRoot中的一个字符串匹配时,才会修改bind元素的ref属性。此变量将是动态的,并将在生产环境中保存不同数量的字符串。我当前的解决方案只适用于一个字符串,但当我开始将其应用于变量中的多个字符串时,“Others”-子句掩盖了修改,除非匹配的是最后一个字符串。如何分解最后的“xsl:copy of”-语句,以便仅当变量oldRoot中的字符串都不匹配时才应用?我只能访问XSLT1.0

简化要求:我希望XSLT编辑bind元素的ref属性,其中开头匹配“list variable”oldRoot中的一个字符串。编辑应将字符串“_1”添加到匹配字符串的末尾

输入XML示例:

<Root>
<field>
    <bind ref="$.First.something.other"/>
</field>
<field>
    <bind ref="$.Second.subcat.name"/>
</field>
<field>
    <bind ref="$.Third.subcat.name"/>
</field>
<field>
    <bind ref="$.First"/>
    <field>
        <bind ref="$.subcat.date"/>
    </field>
</field>
<field>
    <bind ref="$.Third.subcat.date"/>
</field>
</Root>

简化XSL:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:variable name="oldRoot" >
        <block>$.First</block>
        <block>$.Second</block>
    </xsl:variable> 
    <xsl:variable name="end" select="_1"/>

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

<!--    Rewrite attribute ref to work with new XML.-->
    <xsl:template match="*[local-name()='bind']/@ref">
        <xsl:variable name="attribute" select="."/>
        <xsl:for-each select="$oldRoot/block">
            <xsl:choose>
                <xsl:when test="starts-with(substring-after($attribute,.),'.') or $attribute=.">
                    <xsl:attribute name="ref">
                        <xsl:value-of select="."/>
                        <xsl:value-of select="$end"/>       
                        <xsl:value-of select="substring-after($attribute, .)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$attribute"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

美元优先
美元秒
我需要的结果:

<Root>
<field>
    <bind ref="$.First_1.something.other"/>
</field>
<field>
    <bind ref="$.Second_1.subcat.name"/>
</field>
<field>
    <bind ref="$.Third.subcat.name"/>
</field>
<field>
    <bind ref="$.First_1"/>
    <field>
        <bind ref="$.subcat.date"/>
    </field>
</field>
<field>
    <bind ref="$.Third.subcat.date"/>
</field>
</Root>

我建议您这样做:

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="oldRoot" >
    <block>$.First</block>
    <block>$.Second</block>
</xsl:variable> 

<xsl:variable name="end" select="'_1'"/>

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

<xsl:template match="bind/@ref">
    <xsl:variable name="match" select="exsl:node-set($oldRoot)/block[starts-with(current(), .)]"/>
    <xsl:choose>
        <xsl:when test="$match">
            <xsl:attribute name="ref">
                <xsl:value-of select="$match"/>
                <xsl:value-of select="$end"/>
                <xsl:value-of select="substring-after(., $match)"/>
            </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

美元优先
美元秒
应用于输入示例,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <field>
    <bind ref="$.First_1.something.other"/>
  </field>
  <field>
    <bind ref="$.Second_1.subcat.name"/>
  </field>
  <field>
    <bind ref="$.Third.subcat.name"/>
  </field>
  <field>
    <bind ref="$.First_1"/>
    <field>
      <bind ref="$.subcat.date"/>
    </field>
  </field>
  <field>
    <bind ref="$.Third.subcat.date"/>
  </field>
</Root>


注意
中添加的引号



与您的问题无关,但您不应使用类似于
*[local-name()='bind']
的黑客攻击。如果您的输入使用名称空间,请让您的样式表也使用它。

您能用简单的话解释一下您希望在这里应用的规则吗?另外,请澄清您使用的是哪个XSLT处理器:您的样式表被标记为
version=“1.0”
,但需要运行XSLT 2.0处理器。您为什么说这需要XSLT 2.0?(我将在最初的帖子中更好地解释我的规则。因为
在XSLT 1.0中将失败,除非您首先将
$oldRoot
变量转换为节点集。好的,这很奇怪。我使用此样式表在XMLSpy中进行调试,并且它没有对for-each进行投诉。那么您就没有使用XSLT 1.0处理器。我需要使用“hack”,因为我不知道我将遇到什么名称空间。这是非常罕见的,而且根本不应该发生。源XML使用的名称空间是源XML架构的一部分。如果您不知道将遇到的名称空间,那么您怎么知道元素和属性名及其层次结构?