我試圖查看輸入objectType,value0是否存在於Types/Type中,並檢查條件以在輸出中使用Type/Type的兄弟屬性名稱值XML。 下面是輸入XML:如何將xml字符串屬性分配給xslt中的變量
<?xml version="1.0" encoding="UTF-8"?>
<outPut>
<object>
<objectType>TestOne</objectType>
<Attributes>
<attribute name="value0">codeOne</attribute>
<attribute name="value1">35</attribute>
<attribute name="value2">35</attribute>
</Attributes>
<objectType>TestTwo</objectType>
<Attributes>
<attribute name="value0">codeTwo</attribute>
<attribute name="value1">25</attribute>
<attribute name="value2">35</attribute>
</Attributes>
<objectType>TestThree</objectType>
<Attributes>
<attribute name="value0">codeThree</attribute>
<attribute name="value1">25</attribute>
<attribute name="value2">3225</attribute>
<attribute name="value3">225</attribute>
</Attributes>
<objectType>TestFour</objectType>
<Attributes>
<attribute name="value0">codeFour</attribute>
<attribute name="value1">25</attribute>
<attribute name="value2">35</attribute>
</Attributes>
<objectType>TestFive</objectType>
<Attributes>
<attribute name="value0">codeFive</attribute>
<attribute name="value1">2</attribute>
<attribute name="value2">3225</attribute>
<attribute name="value3">225</attribute>
</Attributes>
<objectType>TestSix</objectType>
<Attributes>
<attribute name="value0">codSix</attribute>
<attribute name="value1">2</attribute>
<attribute name="value2">3225</attribute>
<attribute name="value3">225</attribute>
</Attributes>
</object>
<Types>
<Type>
<temp>TestOne</temp>
<ID>2847</ID>
<Is_Ingest>0</Is_Ingest>
<Category>NAV</Category>
<CategoryTwo>NAVAid</CategoryTwo>
<Class>codeOne</Class>
<New_Attribute>nisting</New_Attribute>
<New_Value>52</New_Value>
<Condition>value1=35</Condition>
</Type>
<Type>
<temp>TestTwo</temp>
<ID>2847</ID>
<Is_Ingest>0</Is_Ingest>
<Category>NAV</Category>
<CategoryTwo>NAVAid</CategoryTwo>
<Class>codeTwo</Class>
<New_Attribute>nisting</New_Attribute>
<New_Value>53</New_Value>
<Condition>value1!=33</Condition>
</Type>
<Types>
<Type>
<temp>TestThree</temp>
<ID>28247</ID>
<Is_Ingest>0</Is_Ingest>
<Category>NAV</Category>
<CategoryTwo>NAVAid</CategoryTwo>
<Class>codeThree</Class>
<New_Attribute>nisting</New_Attribute>
<New_Value>52</New_Value>
<Condition>value1=35</Condition>
</Type>
<Type>
<temp>TestFour</temp>
<ID>2847</ID>
<Is_Ingest>0</Is_Ingest>
<Category>NAV</Category>
<CategoryTwo>NAVAid</CategoryTwo>
<Class>codeFour</Class>
<New_Attribute>AidM</New_Attribute>
<New_Value>45</New_Value>
<Condition>value1!=33 & value1!=28</Condition>
</Type>
<Type>
<temp>TestFive</temp>
<ID>2847</ID>
<Is_Ingest>0</Is_Ingest>
<Category>NAV</Category>
<CategoryTwo>Aid</CategoryTwo>
<Class>codeFive</Class>
<New_Attribute>AidM</New_Attribute>
<New_Value>4</New_Value>
<Condition>!value</Condition>
</Type>
<Type>
<temp>TestSix</temp>
<ID>2847</ID>
<Is_Ingest>0</Is_Ingest>
<Category>NAV</Category>
<CategoryTwo>Aid</CategoryTwo>
<Class>codeSix</Class>
<New_Attribute>AidM</New_Attribute>
<New_Value>4</New_Value>
</Type>
</Types>
</outPut>
這裏是xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/Input">
<objects>
<xsl:for-each select="Output">
<object type="{object/objectType}">
<xsl:variable name="attributes" select="Attributes/*"/>
<xsl:variable name="matching-template" select="/Output/Types/Type[temp=$attributes and class=$attributes]"/>
<template>
<xsl:value-of select="$matching-template/New_Attribute"/>
<xsl:value-of select="$matching-template/New_Value"/>
</template>
</object>
</xsl:for-each>
</objects>
</xsl:template>
</xsl:stylesheet>
預期輸出
<output>
<object>
<objectType>TestOne</objectType>
<Attributes>
<attribute name="value0">codeOne</attribute>
<attribute name="value1">25</attribute>
<attribute name="value2">35</attribute>
<attribute name="nisting">52</attribute>
</Attributes>
<objectType>TestTwo</objectType>
<Attributes>
<attribute name="value0">codeTwo</attribute>
<attribute name="value1">25</attribute>
<attribute name="value2">35</attribute>
<attribute name="NAV">NAVAID</attribute>
<attribute name="value2">35</attribute>
</Attributes>
</object>
</output>
您是否試圖在XSLT中構建表達式解析器?這將是一個相當努力!或者你只是試圖在''中標記字符串?在這兩種情況下,我都無法將您的建議結果與輸入相匹配。請更確切地說,仔細檢查你的問題。 – 2014-09-23 22:38:52
您知道,通過您在此處所做的所有努力,以不同的用戶名稱詢問和重複詢問相同的問題,我不知道多少次......如果您已將這些努力花在實際**學習XSLT * *,你現在已經是專家了。 – 2014-10-04 09:25:57