在xslt中使用模板的匹配属性中的变量

时间:2016-06-09 19:20:33

标签: xslt

这是我要转换的xml文件

  <ent>  
   <externalId></externalId>
      <productKey>
        <startDate>2016-06-07</startDate>
        <endDate>2050-12-31</endDate>
        <Item>
          <enforcementIdentifier>
            <enforcementName>DEMO License Generator</enforcementName>
            <enforcementVersion>1.0</enforcementVersion>
          </enforcementIdentifier>
          <totalQuantity>10485760</totalQuantity>
          <availableQuantity>10485760</availableQuantity>
          <activationMethod>FIXED</activationMethod>
          <fixedQuantity>1</fixedQuantity>
          <itemState>2</itemState>
          <product>
            <productIdentifier>
              <prdExternalId>100400-20</prdExternalId>
              <productId>3</productId>
              <productNameVersion>
                <productName>connect-InShere-Monthly</productName>
                <productVersion>1.0</productVersion>
              </productNameVersion>
            </productIdentifier>
            <feature>
              <featureIdentifier>
                <ftrExternalId></ftrExternalId>
                <featureId>2</featureId>
                <featureIdentity>2</featureIdentity>
                <ftrNameVersion>
                  <featureName>Monthly Subscription 1.0</featureName>
                  <featureVersion></featureVersion>
                </ftrNameVersion>
              </featureIdentifier>
              <licenseModel>
                <licenseModelIdentifier>
                  <licenseModelId>2</licenseModelId>
                  <licenseModelName>Days based</licenseModelName>
                </licenseModelIdentifier>
                <attribute>
                  <Name>Number of Days</Name>
                  <value>30</value>
                </attribute>
                <attribute>
                  <Name>Enforce Clock Tamper</Name>
                  <value>1</value>
                </attribute>
                <attribute>
                  <Name>Concurrent Users</Name>
                  <value>5</value>
                </attribute>
              </licenseModel>
              <itemFeatureState>INCLUDED</itemFeatureState>
            </feature>
          </product>
          <commonLicenseAttributes>
            <attribute>
              <Name>Locking Code</Name>
              <value>0</value>
            </attribute>
          </commonLicenseAttributes>
          <activationAttributes>
            <attributeGroup groupName="LOCKING">
              <attribute>
                <attributeName>Locking Code</attributeName>
                <attributeValue>0</attributeValue>
                <readOnly>false</readOnly>
                <mandatory>true</mandatory>
              </attribute>
            </attributeGroup>
          </activationAttributes>
          <entitlementItemAttributes></entitlementItemAttributes>
          <customAttribute>
            <name>Source</name>
            <value>Aarthy Testing</value>
          </customAttribute>
        </Item>
      </productKey>
      <productKey>
        <startDate>2016-06-07</startDate>
        <endDate>2050-12-31</endDate>
        <Item>
          <enforcementIdentifier>
            <enforcementName>DEMO License Generator</enforcementName>
            <enforcementVersion>1.0</enforcementVersion>
          </enforcementIdentifier>
          <totalQuantity>10485760</totalQuantity>
          <availableQuantity>10485760</availableQuantity>
          <activationMethod>FIXED</activationMethod>
          <fixedQuantity>1</fixedQuantity>
          <itemState>2</itemState>
          <product>
            <productIdentifier>
              <prdExternalId>SP001</prdExternalId>
              <productId>2</productId>
              <productNameVersion>
                <productName>Sample-Product</productName>
                <productVersion>1</productVersion>
              </productNameVersion>
            </productIdentifier>
            <feature>
              <featureIdentifier>
                <ftrExternalId></ftrExternalId>
                <featureId>1</featureId>
                <featureIdentity>1</featureIdentity>
                <ftrNameVersion>
                  <featureName>SampleFeature</featureName>
                  <featureVersion></featureVersion>
                </ftrNameVersion>
              </featureIdentifier>
              <licenseModel>
                <licenseModelIdentifier>
                  <licenseModelId>2</licenseModelId>
                  <licenseModelName>Days based</licenseModelName>
                </licenseModelIdentifier>
                <attribute>
                  <Name>Number of Days</Name>
                  <value>30</value>
                </attribute>
                <attribute>
                  <Name>Enforce Clock Tamper</Name>
                  <value>1</value>
                </attribute>
                <attribute>
                  <Name>Concurrent Users</Name>
                  <value>5</value>
                </attribute>
              </licenseModel>
              <itemFeatureState>INCLUDED</itemFeatureState>
            </feature>
          </product>
          <commonLicenseAttributes>
            <attribute>
              <Name>Locking Code</Name>
              <value>0</value>
            </attribute>
          </commonLicenseAttributes>
          <activationAttributes>
            <attributeGroup groupName="LOCKING">
              <attribute>
                <attributeName>Locking Code</attributeName>
                <attributeValue>0</attributeValue>
                <readOnly>false</readOnly>
                <mandatory>true</mandatory>
              </attribute>
            </attributeGroup>
          </activationAttributes>
          <entitlementItemAttributes></entitlementItemAttributes>
          <customAttribute>
            <name>Source</name>
            <value>Aarthy Testing</value>
          </customAttribute>
        </Item>
      </productKey>
      <entitlementAttributes></entitlementAttributes>
    </ent>

我想根据<endDate>下的<prdExternalId>更新<productKey>。我试过的xslt是

<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="endDate"><!-- can be global -->
<xsl:text>ssssssss</xsl:text>
</xsl:variable>
<xsl:variable name="sku"><!-- can be global -->
<xsl:text>100400-20</xsl:text>
</xsl:variable>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>

       <xsl:apply-templates select="node()|@*"/>

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

 <xsl:template match="entitlement/productKey/endDate[../child::Item/child::product/child::productIdentifier/child::prdExternalId='100400-20']">
<xsl:copy>
  <xsl:value-of select='$endDate' />
</xsl:copy>
 </xsl:template>


</xsl:stylesheet> 
当我传递常数时,

有效..但我尝试传递$ sku(如下所示)

<xsl:template match="entitlement/productKey/endDate[../child::Item/child::product/child::productIdentifier/child::prdExternalId=$sku]">
        <xsl:copy>
              <xsl:value-of select='$endDate' />
            </xsl:copy>
             </xsl:template>

变量而不是常量它不起作用。请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

在XSLT 1.0中,您不能在匹配模式中使用变量。

尝试改为:

<xsl:template match="endDate">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="../Item/product/productIdentifier/prdExternalId=$sku">
                <xsl:value-of select="$endDate" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="." />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>