【问题标题】:Get Attributes from XML using XSLT and parameters使用 XSLT 和参数从 XML 获取属性
【发布时间】:2014-10-20 12:39:23
【问题描述】:

我做了这个 xslt,它从 XML 中获取一个值,我有参数可以发送到 for each 以获得我需要的东西,它没有问题,我的 for each 做一个比较,如果值匹配我的参数然后它就完成了。

有人知道这是否是一个好方法吗?我这么说是因为我是 XSLT 的新手,这就是我认为可能没有问题的方式。我想知道一个函数在编码和速度方面是否会是一个更好的解决方案。

干杯!

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="TransactionDataOfRequest">
  <xsl:output method="text" indent="no"/>
  <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:param name="param1" select="'ADM_1'"/>
    <xsl:param name="param2" select="'ADM_2'"/>
    <xsl:for-each select="//x:form/x:add">
      <xsl:if test="@name=$param1">
        Question1=<xsl:value-of select="."/>
      </xsl:if>
    </xsl:for-each>

    <xsl:for-each select="//x:form/x:add">
      <xsl:if test="@name=$param2">
        <xsl:variable name="test">
          <xsl:value-of select="."/>
        </xsl:variable>
        Question2=<xsl:copy-of select="$test" />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="iso-8859-1"?>
<transaction xmlns="TransactionDataOfRequest" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <success>true</success>
  <code>0</code>
  <value>
    <form>
      <add name="ADM_1" title="Question 1" type="String" isList="false">No</add>
      <add name="ADM_2" title="Question 2" type="String" isList="false">Yes</add>
    </form>
  </value>
</transaction>

【问题讨论】:

    标签: xml function xslt


    【解决方案1】:

    假设您想打印所有问题和答案,您可以使用一个简单的应用模板进行一般性操作:

    <?xml version="1.0"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="TransactionDataOfRequest">
        <xsl:output method="text" indent="no"/>
        <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="/">
            <xsl:apply-templates select="//x:form/x:add"></xsl:apply-templates>
        </xsl:template>
    
        <xsl:template match="x:add">
            <xsl:value-of select="@title"/>=<xsl:value-of select="."/>
            <xsl:text>&#xa;</xsl:text>
        </xsl:template>
    
    </xsl:stylesheet> 
    

    编辑

    您可以强制执行此操作,并且我猜仍然可以将其干燥:

    <?xml version="1.0"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="TransactionDataOfRequest">
        <xsl:output method="text" indent="no"/>
        <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="/">
            <xsl:call-template name="processQuestion">
                <xsl:with-param name="name" select="'ADM_1'"></xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="processQuestion">
                <xsl:with-param name="name" select="'ADM_2'"></xsl:with-param>
            </xsl:call-template>
        </xsl:template>
    
        <xsl:template name="processQuestion">
            <xsl:param name="name"></xsl:param>
            <xsl:apply-templates select="//x:form/x:add[@name=$name]" mode="process" />
        </xsl:template>
    
        <xsl:template match="x:add" mode="process">
            <xsl:value-of select="@title"/>=<xsl:value-of select="."/>
            <xsl:text>&#xa;</xsl:text>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 您好 StuartLC,感谢您的回复,稍后我需要对元素进行操作,因此需要单独获取它们。这就是我使用参数的原因。
    • 我已经更新了对调用模板(函数)的显式调用。您仍然可以避免for-each、手动循环匹配和局部变量。
    • 感谢 Stuart,这就是我一直在寻找的解决方案!
    猜你喜欢
    • 2016-05-16
    • 2021-10-18
    • 2013-08-23
    • 2016-11-03
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多