Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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 需要从XSL中获取属性值_Xml_Xslt_Attributes_Xslt 2.0 - Fatal编程技术网

Xml 需要从XSL中获取属性值

Xml 需要从XSL中获取属性值,xml,xslt,attributes,xslt-2.0,Xml,Xslt,Attributes,Xslt 2.0,我一直致力于生成元素和属性名称以及输出元素和属性。在这里,我使用OxyXMLEditor 18.0将输入xml转换为DocBookXML 我的输入xml如下所示: <Section1 id="1"> <Section1Heading>Section 1</Section1Heading> <Section2 id="2"> <Section2Heading>Heading</Section2Heading>

我一直致力于生成元素和属性名称以及输出元素和属性。在这里,我使用OxyXMLEditor 18.0将输入xml转换为DocBookXML

我的输入xml如下所示:

<Section1 id="1">
   <Section1Heading>Section 1</Section1Heading>
  <Section2 id="2">
    <Section2Heading>Heading</Section2Heading>
    <Para>other.</Para>
  </Section2>
</Section1>

我认为这将产生您需要的输出,尽管它依赖于具有非常有限结构的输入XSLT(例如,它假定
xsl:attribute
始终包含
xsl:value



使用
match=“Section1”
我不知道
Section1/@id
会选择什么,没有嵌套的
Section1
元素。我现在已经编辑了。请see@MartinHonnen预期输出元素和属性的顺序也不是问题。如果它重新排序也没有问题,我会得到像
标签、@id
这样的属性。但我需要它作为
@id,label
。因为输入是@id,输出是label,我修改了我的答案,在相关的
concat
语句中交换两个参数,所以它们现在应该是正确的顺序。
<xsl:template match="Section1">
   <sect1>
       <xsl:attribute name="label">
          <xsl:value-of select="@id"/>
       </xsl:attribute>
      <xsl:apply-templates/>
   </sect1>
</xsl:template>

<xsl:template match="Section1Heading | Section2Heading">
    <title>
        <xsl:apply-templates/>
    </title>
</xsl:template>

<xsl:template match="Section2">
   <sect2>
      <xsl:attribute name="label">
         <xsl:value-of select="@id"/>
      </xsl:attribute>
      <xsl:apply-templates/>
   </sect2>
</xsl:template>

<xsl:template match="Para">
   <para>
      <xsl:apply-templates/>
   </para>
</xsl:template>
  <xsl:param name="field-sep" as="xs:string">,</xsl:param>
  <xsl:param name="line-sep" as="xs:string" select="'&#10;'"/>

  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
      <xsl:value-of select="concat('Input XML', $field-sep, 'Result XML')"/>
      <xsl:value-of select="$line-sep"/>
      <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="xsl:template[@match]">
    <xsl:value-of 
       select="for $pattern in tokenize(@match, '\s*\|\s*')
               return concat($pattern, $field-sep, node-name(current()/*[1]))"
       separator="{$line-sep}"/>
       <xsl:value-of select="$line-sep"/>
  </xsl:template>
Input XML,Result XML
Section1,sect1
@id,label
Section1Heading,title
Section2Heading,title
Section2,sect2
@id,label
Para,para
<xsl:template match="xsl:template[@match]">
  <xsl:variable name="current" select="." />
  <xsl:for-each select="tokenize(@match, '\s*\|\s*')">
    <xsl:value-of select="concat(., $field-sep, node-name($current/*[1]))" />
    <xsl:value-of select="$line-sep"/>
    <xsl:for-each select="$current/*[1]/xsl:attribute">
      <xsl:value-of select="concat(xsl:value-of[1]/@select, $field-sep, @name)" />
      <xsl:value-of select="$line-sep"/>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>