XSLT:从一个与另一个与变换/映射合并使用属性的两个标签,标签
我在输入文件,变量和类型两个标记:XSLT:从一个与另一个与变换/映射合并使用属性的两个标签,标签
<variable baseType="int" name="X"> </variable> 
<type baseType="structure" name="Y"> 
    <variableInstance name="X" /> 
</type> 
,我需要生成以下输出文件:
<Item name="Y">     <Field name="X" type="Long" /> 
</Item> 
所以这里概念我的做法一直类型标签转换成项目踏歌,变量实例字段。这是工作的罚款:
<xsl:for-each select="type[@baseType='structure']">     <Item> 
     <xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute> 
     <xsl:for-each select="variableInstance"> 
      <Field> 
       <xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute> 
       <xsl:attribute name="type">**THIS IS WHERE I'M STUCK**</xsl:attribute> 
      </Field> 
     </xsl:for-each> 
    </Item> 
</xsl:for-each> 
我卡上的问题是:
- 我不知道如何让variableInstance /字段标签匹配的名字变量标签,所以我可以访问baseType。
- 我要地图“INT”,以“龙”一旦我能够做到1
提前感谢!
回答:
问题1.
对于你有,你可以使用一个关键的第一个问题:
<xsl:key name="variable-key" match="//variable" use="@name" /> 这关键是要索引文档中的所有变量元素,用他们的名字。所以,现在,我们可以通过下面的XPath表达式访问任何这些元素:
key('variable-key', 'X') 使用这种方法是有效的,当你有很多可变因素。
注意:如果每个变量都有自己的作用域(即局部变量在文档的不同部分不可见),则此方法无效。在这种情况下,应该修改这种方法。
问题2.
对于映射属性,你可以使用模板类似如下:
<xsl:template match="@baseType[. = 'int']">      <xsl:attribute name="baseType"> 
      <xsl:value-of select="'Long'" /> 
     </xsl:attribute> 
    </xsl:template> 
这种转变的含义是:每次我们为int值BASETYPE属性与时间,它必须被Long值所取代。
此转换将适用于文档中的每个@baseType属性。
使用描述策略的解决方案可能是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:output method="xml" indent="yes" /> 
    <!-- Index all variable elements in the document by name --> 
    <xsl:key name="variable-key" 
      match="//variable" 
      use="@name" /> 
    <!-- Just for demo --> 
    <xsl:template match="text()" /> 
    <!-- Identity template: copy attributes by default --> 
    <xsl:template match="@*"> 
     <xsl:copy> 
      <xsl:value-of select="." /> 
     </xsl:copy> 
    </xsl:template> 
    <!-- Match the structure type --> 
    <xsl:template match="type[@baseType='structure']"> 
     <Item> 
      <xsl:apply-templates select="*|@*" /> 
     </Item> 
    </xsl:template> 
    <!-- Match the variable instance --> 
    <xsl:template match="variableInstance"> 
     <Field> 
      <!-- Use the key to find the variable with the current name --> 
      <xsl:apply-templates select="@*|key('variable-key', @name)/@baseType" /> 
     </Field> 
    </xsl:template> 
    <!-- Ignore attributes with baseType = 'structure' --> 
    <xsl:template match="@baseType[. = 'structure']" /> 
    <!-- Change all baseType attributes with long values to an attribute 
     with the same name but with an int value --> 
    <xsl:template match="@baseType[. = 'int']"> 
     <xsl:attribute name="baseType"> 
      <xsl:value-of select="'Long'" /> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 
这代码将下面的XML文档转换:
<!-- The code element is present just for demo --> <code> 
    <variable baseType="int" name="X" /> 
    <type baseType="structure" name="Y"> 
     <variableInstance name="X" /> 
    </type> 
</code> 
到
<Item name="Y">     <Field baseType="Long" name="X"/> 
</Item> 
回答:
冲电气我有一个解决方案t 1 xsltcake slice或with use of templates。对于第二点,我可能会使用与Pablo Pozo在他的回答中使用的模板类似的模板
以上是 XSLT:从一个与另一个与变换/映射合并使用属性的两个标签,标签 的全部内容, 来源链接: utcz.com/qa/261725.html





