Как переименовать имя умножения атрибутов в XSLT 3.0? - PullRequest
0 голосов
/ 06 мая 2020

это мой основной xml:
<tag tag_id="1452" type="0" date="2016-04-25" attr="20" flag="0" ntef="28" norfe="1934" kol="0" op="0" mid="" max="1" min"0.00" ... />
Второй (файл. xml):

<tt>
    <tag>
        <name2>value</name2>
        <name1>ntef</name1>
    </tag>
    ...
</tt>

Я хочу переименовать атрибуты в основном xml если они существуют во втором xml теге «name1». Новое имя получается из name2.
My xslt:

<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" indent="no" encoding="windows-1251"/>

<xsl:template match="/">
     <xsl:apply-templates select="//*"/>
</xsl:template>

<xsl:template match="//*">
    <xsl:copy> 
        <xsl:value-of select="concat('Title ', local-name(),'&#xA;')"/>

        <xsl:copy>
            <xsl:apply-templates mode="language" select="@*"/>
        </xsl:copy>

        <xsl:apply-templates mode="toText" select="@*"/>

    </xsl:copy> 
</xsl:template>

<xsl:template match="." mode="language">
    <xsl:variable name="attr" select="local-name()" />
    <xsl:variable name="data" select="document('file.xml')/tt/tag[name1=$attr]" />

        <xsl:variable name="name"  select="$data/name2)" />
        <xsl:attribute name="{$name}">
          <xsl:value-of select="."/>
        </xsl:attribute>
        <xsl:apply-templates />

</xsl:template>

<xsl:template match="@*" mode="toText">
        <xsl:value-of select="concat(local-name(),'=', ., '&#xA;'" />   
</xsl:template>
</xsl:stylesheet>

Код выполняется без ошибок, но на выходе нет переименованных атрибутов. Я не могу указать имя атрибута, потому что его слишком много.

1 Ответ

1 голос
/ 06 мая 2020

Используйте ключ и проверьте, есть ли ссылка:

<?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:param name="transformation-doc" select="doc('tt.xml')"/>

  <xsl:key name="att-ref" match="tt/tag/name2" use="../name1"/>

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

  <xsl:template match="tag/@*[key('att-ref', name(), $transformation-doc)]">
      <xsl:attribute name="{key('att-ref', name(), $transformation-doc)}" select="."/>
  </xsl:template>

</xsl:stylesheet>

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...