按两个条件分组在一个元素中

huangapple go评论92阅读模式
英文:

Group-by two conditions in one element

问题

以下是您要翻译的部分:

"I am trying to group-by two conditions in one element, which practically means that machine and parts with same ID (before '-' should be grouped.

Input:

  1. <Document>
  2. <DocumentLine>
  3. <ExternalBatchNo>KEC36770</ExternalBatchNo>
  4. <Attribute01>machine</Attribute01>
  5. </DocumentLine>
  6. <DocumentLine>
  7. <ExternalBatchNo>KEC36770-2</ExternalBatchNo>
  8. <Attribute01>part</Attribute01>
  9. </DocumentLine>
  10. <DocumentLine>
  11. <ExternalBatchNo>KEC36771</ExternalBatchNo>
  12. <Attribute01>machine</Attribute01>
  13. </DocumentLine>
  14. <DocumentLine>
  15. <ExternalBatchNo>KEC36771-2</ExternalBatchNo>
  16. <Attribute01>part</Attribute01>
  17. </DocumentLine>
  18. <DocumentLine>
  19. <ExternalBatchNo>KEC36771-3</ExternalBatchNo>
  20. <Attribute01>part</Attribute01>
  21. </DocumentLine>
  22. </Document>

Expected result:

  1. <Document>
  2. <DocumentLine>
  3. <ExternalBatchNo>KEC36770</ExternalBatchNo>
  4. <Attribute01>machine</Attribute01>
  5. </DocumentLine>
  6. <DocumentLine>
  7. <ExternalBatchNo>KEC36770-2</ExternalBatchNo>
  8. <Attribute01>part</Attribute01>
  9. </DocumentLine>
  10. </Document>
  11. <Document>
  12. <DocumentLine>
  13. <ExternalBatchNo>KEC36771</ExternalBatchNo>
  14. <Attribute01>machine</Attribute01>
  15. </DocumentLine>
  16. <DocumentLine>
  17. <ExternalBatchNo>KEC36771-2</ExternalBatchNo>
  18. <Attribute01>part</Attribute01>
  19. </DocumentLine>
  20. <DocumentLine>
  21. <ExternalBatchNo>KEC36771-3</ExternalBatchNo>
  22. <Attribute01>part</Attribute01>
  23. </DocumentLine>
  24. </Document>

This is what I tried, but this results in an empty machine ID and so only groups on parts.

  1. <xsl:for-each-group select="Message/Documents/Document/DocumentLine" group-by="substring-before(ExternalBatchNo,'-')">

So I am thinking about using some template where he should pick the ExternalBatchNo when value doesn't contain an '-'. But, how?"

英文:

I am trying to group-by two conditions in one element, which practically means that machine and parts with same ID (before '-' should be grouped.

Input:

  1. <Document>
  2. <DocumentLine>
  3. <ExternalBatchNo>KEC36770</ExternalBatchNo>
  4. <Attribute01>machine</Attribute01>
  5. </DocumentLine>
  6. <DocumentLine>
  7. <ExternalBatchNo>KEC36770-2</ExternalBatchNo>
  8. <Attribute01>part</Attribute01>
  9. </DocumentLine>
  10. <DocumentLine>
  11. <ExternalBatchNo>KEC36771</ExternalBatchNo>
  12. <Attribute01>machine</Attribute01>
  13. </DocumentLine>
  14. <DocumentLine>
  15. <ExternalBatchNo>KEC36771-2</ExternalBatchNo>
  16. <Attribute01>part</Attribute01>
  17. </DocumentLine>
  18. <DocumentLine>
  19. <ExternalBatchNo>KEC36771-3</ExternalBatchNo>
  20. <Attribute01>part</Attribute01>
  21. </DocumentLine>
  22. </Document>

Expected result:

  1. <Document>
  2. <DocumentLine>
  3. <ExternalBatchNo>KEC36770</ExternalBatchNo>
  4. <Attribute01>machine</Attribute01>
  5. </DocumentLine>
  6. <DocumentLine>
  7. <ExternalBatchNo>KEC36770-2</ExternalBatchNo>
  8. <Attribute01>part</Attribute01>
  9. </DocumentLine>
  10. </Document>
  11. <Document>
  12. <DocumentLine>
  13. <ExternalBatchNo>KEC36771</ExternalBatchNo>
  14. <Attribute01>machine</Attribute01>
  15. </DocumentLine>
  16. <DocumentLine>
  17. <ExternalBatchNo>KEC36771-2</ExternalBatchNo>
  18. <Attribute01>part</Attribute01>
  19. </DocumentLine>
  20. <DocumentLine>
  21. <ExternalBatchNo>KEC36771-3</ExternalBatchNo>
  22. <Attribute01>part</Attribute01>
  23. </DocumentLine>
  24. </Document>

This is what I tried, but this results in an empty machine ID and so only groups on parts.

  1. <xsl:for-each-group select="Message/Documents/Document/DocumentLine" group-by="substring-before(ExternalBatchNo,'-')">

So I am thinking about using some template where he should pick the ExternalBatchNo when value doesn't contain an '-'. But, how?

答案1

得分: 0

如果您可以使用XSLT版本2.0,您可以使用以下方式:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. version="2.0">
  5. <xsl:output indent="yes"/>
  6. <xsl:strip-space elements="*"/>
  7. <xsl:template match="Documents">
  8. <xsl:copy>
  9. <!-- 您的 `substring-before` 在没有短横线的字符串上不起作用。请尝试使用 tokenize 替代 -->
  10. <xsl:for-each-group select="Document/DocumentLine" group-by="tokenize(ExternalBatchNo,'-')[1]">
  11. <Document>
  12. <xsl:copy-of select="current-group()"/>
  13. </Document>
  14. </xsl:for-each-group>
  15. </xsl:copy>
  16. </xsl:template>
  17. </xsl:stylesheet>

使用此源XML:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Message>
  3. <Documents>
  4. <Document>
  5. <DocumentLine>
  6. <ExternalBatchNo>KEC36770</ExternalBatchNo>
  7. <Attribute01>machine</Attribute01>
  8. </DocumentLine>
  9. <DocumentLine>
  10. <ExternalBatchNo>KEC36770-2</ExternalBatchNo>
  11. <Attribute01>part</Attribute01>
  12. </DocumentLine>
  13. <DocumentLine>
  14. <ExternalBatchNo>KEC36771</ExternalBatchNo>
  15. <Attribute01>machine</Attribute01>
  16. </DocumentLine>
  17. <DocumentLine>
  18. <ExternalBatchNo>KEC36771-2</ExternalBatchNo>
  19. <Attribute01>part</Attribute01>
  20. </DocumentLine>
  21. <DocumentLine>
  22. <ExternalBatchNo>KEC36771-3</ExternalBatchNo>
  23. <Attribute01>part</Attribute01>
  24. </DocumentLine>
  25. </Document>
  26. </Documents>
  27. </Message>

将产生以下结果:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Documents>
  3. <Document>
  4. <DocumentLine>
  5. <ExternalBatchNo>KEC36770</ExternalBatchNo>
  6. <Attribute01>machine</Attribute01>
  7. </DocumentLine>
  8. <DocumentLine>
  9. <ExternalBatchNo>KEC36770-2</ExternalBatchNo>
  10. <Attribute01>part</Attribute01>
  11. </DocumentLine>
  12. </Document>
  13. <Document>
  14. <DocumentLine>
  15. <ExternalBatchNo>KEC36771</ExternalBatchNo>
  16. <Attribute01>machine</Attribute01>
  17. </DocumentLine>
  18. <DocumentLine>
  19. <ExternalBatchNo>KEC36771-2</ExternalBatchNo>
  20. <Attribute01>part</Attribute01>
  21. </DocumentLine>
  22. <DocumentLine>
  23. <ExternalBatchNo>KEC36771-3</ExternalBatchNo>
  24. <Attribute01>part</Attribute01>
  25. </DocumentLine>
  26. </Document>
  27. </Documents>
英文:

If you can use xslt version 2.0, you could use something like this:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;xsl:stylesheet
  3. xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
  4. version=&quot;2.0&quot;&gt;
  5. &lt;xsl:output indent=&quot;yes&quot;/&gt;
  6. &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
  7. &lt;xsl:template match=&quot;Documents&quot;&gt;
  8. &lt;xsl:copy&gt;
  9. &lt;!-- Your `substring-before` will not work on strings without the dash. Try tokenize instead --&gt;
  10. &lt;xsl:for-each-group select=&quot;Document/DocumentLine&quot; group-by=&quot;tokenize(ExternalBatchNo,&#39;-&#39;)[1]&quot;&gt;
  11. &lt;Document&gt;
  12. &lt;xsl:copy-of select=&quot;current-group()&quot;/&gt;
  13. &lt;/Document&gt;
  14. &lt;/xsl:for-each-group&gt;
  15. &lt;/xsl:copy&gt;
  16. &lt;/xsl:template&gt;
  17. &lt;/xsl:stylesheet&gt;

Using this source xml:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;Message&gt;
  3. &lt;Documents&gt;
  4. &lt;Document&gt;
  5. &lt;DocumentLine&gt;
  6. &lt;ExternalBatchNo&gt;KEC36770&lt;/ExternalBatchNo&gt;
  7. &lt;Attribute01&gt;machine&lt;/Attribute01&gt;
  8. &lt;/DocumentLine&gt;
  9. &lt;DocumentLine&gt;
  10. &lt;ExternalBatchNo&gt;KEC36770-2&lt;/ExternalBatchNo&gt;
  11. &lt;Attribute01&gt;part&lt;/Attribute01&gt;
  12. &lt;/DocumentLine&gt;
  13. &lt;DocumentLine&gt;
  14. &lt;ExternalBatchNo&gt;KEC36771&lt;/ExternalBatchNo&gt;
  15. &lt;Attribute01&gt;machine&lt;/Attribute01&gt;
  16. &lt;/DocumentLine&gt;
  17. &lt;DocumentLine&gt;
  18. &lt;ExternalBatchNo&gt;KEC36771-2&lt;/ExternalBatchNo&gt;
  19. &lt;Attribute01&gt;part&lt;/Attribute01&gt;
  20. &lt;/DocumentLine&gt;
  21. &lt;DocumentLine&gt;
  22. &lt;ExternalBatchNo&gt;KEC36771-3&lt;/ExternalBatchNo&gt;
  23. &lt;Attribute01&gt;part&lt;/Attribute01&gt;
  24. &lt;/DocumentLine&gt;
  25. &lt;/Document&gt;
  26. &lt;/Documents&gt;
  27. &lt;/Message&gt;

will give this result:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;Documents&gt;
  3. &lt;Document&gt;
  4. &lt;DocumentLine&gt;
  5. &lt;ExternalBatchNo&gt;KEC36770&lt;/ExternalBatchNo&gt;
  6. &lt;Attribute01&gt;machine&lt;/Attribute01&gt;
  7. &lt;/DocumentLine&gt;
  8. &lt;DocumentLine&gt;
  9. &lt;ExternalBatchNo&gt;KEC36770-2&lt;/ExternalBatchNo&gt;
  10. &lt;Attribute01&gt;part&lt;/Attribute01&gt;
  11. &lt;/DocumentLine&gt;
  12. &lt;/Document&gt;
  13. &lt;Document&gt;
  14. &lt;DocumentLine&gt;
  15. &lt;ExternalBatchNo&gt;KEC36771&lt;/ExternalBatchNo&gt;
  16. &lt;Attribute01&gt;machine&lt;/Attribute01&gt;
  17. &lt;/DocumentLine&gt;
  18. &lt;DocumentLine&gt;
  19. &lt;ExternalBatchNo&gt;KEC36771-2&lt;/ExternalBatchNo&gt;
  20. &lt;Attribute01&gt;part&lt;/Attribute01&gt;
  21. &lt;/DocumentLine&gt;
  22. &lt;DocumentLine&gt;
  23. &lt;ExternalBatchNo&gt;KEC36771-3&lt;/ExternalBatchNo&gt;
  24. &lt;Attribute01&gt;part&lt;/Attribute01&gt;
  25. &lt;/DocumentLine&gt;
  26. &lt;/Document&gt;
  27. &lt;/Documents&gt;

答案2

得分: 0

另一个简单的解决方案是在开罗放一头大象

  1. group-by=&quot;substring-before(concat(ExternalBatchNo, &#39;-&#39;), &#39;-&#39;)&quot;
英文:

Another simple solution is to place an elephant in Cairo:

  1. group-by=&quot;substring-before(concat(ExternalBatchNo, &#39;-&#39;), &#39;-&#39;)&quot;

huangapple
  • 本文由 发表于 2023年7月20日 16:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定